Ruby on Rails: how to print a line and where does it appear?

I know this is a trivial question. But I have a search all over Google, but I cannot find a simple answer to this question.

Basically, I have a line that says <%= link_to 'Run it', :method => 'doIt' %>in the view, and then in the corresponding controller, I have a method doItas follows:

def doIt
  puts "Just do it" 
end

I just want to check that if I click Run It, it will display the line “Just do it.” I ran this on the local host and there are no errors, but I can’t find the “Just do it” solution anywhere. It does not appear in the console rails or rails log. I just want to know where to put the output of the string, where to find it?


Round 2: So this is what I tried ....

Added this line to index.html.erb (which is the root)

<%= link_to 'Run it', :method => 'do_it' %>

url, http://localhost:3000/ ( route controller # index as root)

- " " , "do_it" .

def do_it
  logger.debug "Just do it"
end

" " , URL- http://localhost:3000/gollum_starters?method=do_it development.log :

Started GET "/gollum_starters?method=do_it" for 127.0.0.1 at 2011-08-25 15:27:49 -0700
  Processing by GollumStartersController#index as HTML
  Parameters: {"method"=>"do_it"}
  [1m[35mGollumStarter Load (0.3ms)[0m  SELECT "gollum_starters".* FROM "gollum_starters"
Rendered gollum_starters/index.html.erb within layouts/application (3.6ms)
Completed 200 OK in 16ms (Views: 7.7ms | ActiveRecord: 0.3ms)

, logger.error/info/fatal/etc... Rails.logger.error/info/fatal/etc, " "

@Paul: , , , rails, ?

@Maz: , , , do_it. - . , , . textmate, IDE.


3:

@Paul alot,

:

resources :gollum_starters

root :to => "gollum_starters#index"

match 'gollum_starters/do_it' => 'gollum_starters#do_it', :as => 'do_it'

index.html.erb:

<%= link_to "Do it", do_it_path %>

gollum_starters_controller.rb

def do_it
  logger.debug 'Just do it'
end

:

GollumStarter ID = do_it

, :

def show
    @gollum_starter = GollumStarter.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @gollum_starter }
    end
  end

, ? do_it, localhost: 3000/gollum_starters/do_it, , , , show?


4:

@Paul, : gollum_starters down:

root :to => "gollum_starters#index"

match 'gollum_starters/do_it' => 'gollum_starters#do_it', :as => 'do_it'

resources :gollum_starters

(omg, ),

gollum_starters/do_it {: handlers = > [: erb,: rjs,: builder,: rhtml,: rxml],: formats = > [: html],: locale = > [: en,: en]} "~/project_name/app/views"

:/

---------- 4 ------------

, , (.. -), , , . , redirect_to, root_url.

def do_it
  logger.debug 'Just do it'
  redirect_to(root_url)
end

, " ", , development.log .

, , , . .

+5
3

, link_to , , , :method, HTTP.

ActionView:: Helpers:: UrlHelper

: . HTTP-. : post,: get,: delete : put. : post.

routes.rb,

# The order of routes is important as the first matched will be used
# therefore the match needs to be above 'resources :controller'
match 'controller/do_it' => 'controller#do_it', :as => 'do_it'

resources :gollum_starters # <--- This needs to be below the match or this will catch first
  • controller/do_it - ,
  • controller#do_it - , , ( #)
  • :as do_it_path, link_to

link_to :

<%= link_to "Do it", do_it_path %>

, view

 app/views/gollum_startes/do_it.html.erb # <-- Add file 

, - , . , .

+4

, "" .

"" , , . RESTful, Rails, : GET, POST, PUT DELETE. .

  • GET = > INDEX SHOW
  • POST = > CREATE
  • PUT = > UPDATE
  • DELETE = > DESTROY

"" , NEW EDIT. GET- . NEW POST (CREATE) , EDIT PUT (UPDATE) .

rails, , HTTP- CRUD.

, , , , GET, POST.

, :

<%= link_to 'Run it', :method => 'do_it' %>

... . HTTP, "do_it", . , Rails URL-. , , , url- ?method=do_it .

, . link_to helper : 1, 2 HREF . , :

link_to 'Run it', url

-, , URL- , .

, : , : controller_name#controller_action. pages#show articles#index.

, ExamplesController, :

link_to 'examples#index', '/examples'
link_to 'examples#show', '/examples/123'  # 123 is the id of a specific example
link_to 'examples#new', '/examples/new'
link_to 'examples#create', '/examples', :method => :post
link_to 'examples#edit', '/examples/123/edit'
link_to 'examples#update', '/examples/123', :method => :put
link_to 'examples#destroy', '/examples/123', :method => :delete

, INDEX, SHOW, NEW EDIT GET. :method => :get,

, Rails .

, , :

link_to 'examples#index', examples_path
link_to 'examples#show', example_path( @example )
link_to 'examples#new', new_example_path
link_to 'examples#create', examples_path, :method => :post
link_to 'examples#edit', edit_example_path( @example )
link_to 'examples#update', example_path( @example ), :method => :put
link_to 'examples#destroy', example_path( @example ), :method => :delete

, routes.rb. , :

resources :examples

... path_helpers .

RESTful , : , (, ) (, ). , , - , , , .

, :

resources :examples do
  collection do
    get 'do_it'
  end
end

, :

resources :examples do
  member do
    get 'do_it'
  end
end

"get" , - GET , , , POST , . :

resources :examples do
  get 'do_it', :on => :collection
  post 'something', :on => :member
end

. .

, , rake routes , . , do_it , : do_it_examples_path.

, , :

<%= link_to 'Do it.', do_it_examples_path %>

... do_it. , puts ( rails s , started GET on examples#do_it...).

, GET , . , , , . , , . :)

, , . .

+2

You want to use the Rails logging mechanism:

http://guides.rubyonrails.org/debugging_rails_applications.html#sending-messages

This means that even if you do not start the server using rails s, the output will still be in the right place.

-1
source

All Articles