Reputation: 3974
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.
Upvotes: 3
Views: 27463
Reputation: 2194
The irb
version 1.6 has been released and allow debugging via binding.irb
. In order to use it, you need to have ruby >= 3.2.
It allows several debugging commands, like show_cmds
, show_doc
, edit
, debug
, break
, catch
, next
, delete
, step
, continue
, finish
, backtrace
, info
. All of them can be seen here:
However, still, at least for now, I prefer to recommend binding.pry
instead of binding.irb
for the debugging process. Using particularly the pry-byebug
gem, which is a gem that enhances the binding.pry
debugger with byebug
capabilities, adding debugging commands like break
, step
, next
, finish
, continue
, backtrace
, up
, down
, frame
, whereami
, ls
, cd
, !!!
, help
, help alias
, help whereami
, w
, @
, $
, @ 5
, h
, $ some_method
, show-source method_name
, find-method to_a
, play -l line_number
, self
, pp(obj)
, cd SomeModule
, ls -m
, cd ..
, b
, s
, c
, n
, and much more...
This debugger provides an also robust list of commands, but more importantly: with less bugs and more intuitive interface at the same time. I will explain these points in more detail in the next section.
binding.irb
X binding.pry
?Which one is better at debugging in rails?
We can start a comparison by a very simple example via rails c
.
binding.irb
exampledef sum(a, b)
binding.irb
puts a
puts b
a + b
end
binding.pry
exampledef sum(a, b)
binding.pry
puts a
puts b
a + b
end
binding.irb
binding.pry
While binding.irb
has a very interesting promise, like serving as the main debugger in rails, it comes with bugs and drawbacks that simply don't exist when we using binding.pry
for the debugging process.
binding.pry
looks more clear for the debugging process, particularly because binding.pry
outputs to the user the code where binding.pry
is located as soon as the code hits this debugger, while binding.irb
doesn't output anything to the user when the code hits this debugger (even if we call whereami
sometimes it doesn't output anything (😱), which is very confusing in terms of usability).
In addition, there are bugs in binding.irb
related to not outputting the value of a variable when we call it! We can check it by looking at the above pictures and noticing the a variable value was not shown to the user here:
irb(main):004> a
On the other hand, binding.pry
doesn't have this bug and always output the value of a variable when we call it and press enter.
As a last note... Is there a way to fix all of these bugs in binding.irb
? In case not, I would recommend to keep using binding.pry
for the debugging process to avoid headaches.
Upvotes: 0
Reputation: 3347
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:
More info about debugging: http://guides.rubyonrails.org/debugging_rails_applications.html
Upvotes: 11
Reputation: 766
Rails.logger.debug "in show method"
etc.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.
Upvotes: 1
Reputation: 21626
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.
Upvotes: 0
Reputation: 516
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.
Upvotes: 0
Reputation: 35054
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.
Upvotes: 2