2

I want to get some lines printed in irb opened through rails console. I have seen a lot of SO questions of how to achieve it. But I get nothing in the irb.

below is the code--

def show
puts 'in show method'
@post = Feed.find_by_id params[:id]
puts @post.inspect
redirect_to root_path unless @post.present? 
end

now I have opened server by command rails server. Also, In another terminal I gave the command rails console, it opened the irb prompt. when in browser I run localhost:3000/posts/82 it gives the correct post, but nothing is shown in the console. What step am I missing? I want to print something in the console when a particular method is called.

inquisitive
  • 3,738
  • 6
  • 30
  • 56
  • You want to use `debugger` or `byebug`. Rails console gives you an access to your app, but not to examining requests. – blelump Nov 03 '14 at 09:29

5 Answers5

10

Best way to debug is to use the debugger command.

If you are using ruby 2.0 or above, you have to use the gem 'byebug' and if you are using 1.9 or below, then gem ruby-debug

then, when you run your server in development mode, your server will stop when it reaches the debugger allowing you to see your objects' state and modify them (much better than simply using puts

The program will stop in the same window that your server runs.

Some basic commands:

  • c continues the execution until next debugger is found
  • n runs the next command. If it is a function executes the function
  • s step into the next command. If it is a function, you will get into the function and see the variables
  • display expression on every step display the result of the expression you write (very useful when debugging loops)
  • undisplay expression_number stops displaying the expresion
  • display shows all the expressions being displayed
  • list Displays the source code being executed
  • help shows the available commands help command_name shows detailed info about a command

More info about debugging: http://guides.rubyonrails.org/debugging_rails_applications.html

Tom Lehman
  • 85,973
  • 71
  • 200
  • 272
Fer
  • 3,247
  • 1
  • 22
  • 33
  • Hi, there, I had took that approach, but though the debugger installed, writing rails server --debugger asked me to install ruby-debug, I tried installing that and it gave me some error, following some SO posts, I instead installed ruby-debug19 and continued by the command `rails server --debugger` to give me the same error – inquisitive Nov 03 '14 at 10:47
  • Theoretically you don't need to start the server with `--debugger` it should take it automatically. Be sure that your `gem 'ruby-debug19'` is in your development group in the gemfile. – Fer Nov 03 '14 at 10:52
  • There's another SO question related with debugging in ruby 1.9 http://stackoverflow.com/questions/1083451/debugging-in-ruby-1-9 take a look to the second answer (it is suggested to use gem 'debugger', group: [:development, :test] instead of ruby-debug19 as it is not longer maintained) – Fer Nov 03 '14 at 10:55
  • Hey thanks, it works now, I was trying to install it using `sudo gem install debugger` instead of putting it in the gem file and doing `sudo bundle install`, since I thought it won't make any difference. Could that be the reason? – inquisitive Nov 03 '14 at 11:07
  • yes, that could be a reason. And I recommend you to run bundle install without sudo... that might be a source of problems too – Fer Nov 03 '14 at 12:00
  • Gotcha! Please also assist me in viewing the contents of puts, if I don't use debugger – inquisitive Nov 03 '14 at 12:20
  • You still can use the debugger (if you are in a loop you can write something like <% debugger if condition %>. As well you can use the `debug` helper to print things in a nicer format ( <%= debug [:a, :b, :c] %>) – Fer Nov 03 '14 at 12:22
  • Can the debugger be used along with feedly_api running as a cron job? The debugger is not launching this way – inquisitive Nov 03 '14 at 12:46
2

The puts 'in show method' in line 2 won't show the output in rails console. Instead it shows the output in the same terminal where you did rails server. They might be lost with so much of output, so try to find it there itself.

Bibek Shrestha
  • 32,848
  • 7
  • 31
  • 34
  • Yeah, it seem to get lost there, Can you give some suggestion on how can I see that since I am new to ubuntu. – inquisitive Nov 03 '14 at 09:45
  • Try using a different logger, may be awesome print. In your Gemfile, add the following `gem 'awesome_print'` and then instead of puts, you can do `ap 'in show method'`. It will show you colored output. – Bibek Shrestha Nov 03 '14 at 09:50
1
  1. Use Rails.logger.debug "in show method" etc.
  2. In the second tab in terminal tail log/development.log like this

    $ cd rails_app_root
    $ tail -f log/development.log
    

    or

    $ cd rails_app_root
    $ less +F log/development.log
    

There you will find all the output from the console.

Sven R.
  • 1,049
  • 17
  • 24
konole
  • 766
  • 4
  • 8
0

Try to use Rails.logger

Rails.logger.info "Some debugging info I want to see in my development log.------#{@post.inspect}"

It will print @post value in log file.

VDN
  • 498
  • 3
  • 15
0

I am a big fan of puts debugging. e.g;

def index
  method = Kernel.instance_method(:method)
  p method.bind(request).call(:headers).source_location
  @users = User.all
end

The above snippet helps you find where the method is implemented:

Processing by UsersController#index as */*
["/Users/aaron/git/rails/actionpack/lib/action_dispatch/http/request.rb", 201]

You can find more cool puts debugging here.

tokhi
  • 21,044
  • 23
  • 95
  • 105