This article is based on the following. https://pikawaka.com/rails/pry
Pry is a function that allows you to use methods etc. on the rails console like Ruby's irb.
pry-rails is a gem ** that allows you to launch pry instead of ** irb when you launch the console. By default, irb starts when you enter console mode with the rails c command. You can use pry by adding gem and executing bundle install as shown below.
Gemfile
#abridgement
gem 'pry-rails`
binding.pry You can stop the process at the point you wrote. I use it for debugging and testing. Debugging is finding non-optional processing (bugs).
books_controller.rb
def create
@book = BookOrder.new(bookorder_params)
binding.pry
if @book.save
redirect_to root_path
else
render :index
end
end
When I stopped the process with binding.pry, the terminal became as follows. The console has started and is waiting for input. You can check the contents by entering "params" and "@book" here.
The following is an example of commands that can be used while executing binding.pry. Please see the official if you like. https://github.com/rweng/pry-rails
Command | Contents |
---|---|
step | step in |
next | step over |
finish | step out |
continue | End debugging (continue interrupted processing) |
[Variable name] | Output the contents of the variable |
$ [Method name] | See method definition |
show-stack | See stacks and races (pry-stack-explorer required) |
!!! | End the process. No matter how many binding.pry there are after that, it can be exited, but rails s etc. ends |
show-routes | Check current routes |
show-models | Check current models |
show-source | Check class, module and method definitions |
show-doc | Check class, module, and method documentation |
At Rails, I feel every day that it is a key to keep track of "the flow of MVC" and "what I am doing for what". I think there are some pains that only a beginner can have, but programming is fun and I think it's the best investment for me in the future, so Let's enjoy learning!
Recommended Posts