[Rails] Introduction of pry-rails ~ How to debug binding.pry

Introduction

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.

Introduction of pry-rails

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. Image from Gyazo


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

in conclusion

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

[Rails] Introduction of pry-rails ~ How to debug binding.pry
[Introduction to Rails] How to use render
How to write Rails
[Rails] Introduction of PAY.JP
How to uninstall Rails
How to use enum (introduction of Japanese notation)
How to use JQuery in js.erb of Rails6
[Rails, JS] How to implement asynchronous display of comments
[Rails] How to change the column name of the table
How to set BuildConfig of library project (module) to Debug
[Rails] How to get the contents of strong parameters
[rails] How to post images
[Rails] How to use enum
[Ruby on Rails] Debug (binding.pry)
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
How to use rails join
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
Introduction to RSpec-Everyday Rails Summary-
[Rails] How to use Scope
Let's summarize how to extend the expiration date of Rails
[Rails] How to display the list of posts by category
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)
[Rails] How to use flash messages
[rails] How to display db information
[Rails] Introduction of Rubocop by beginners
[Rails] How to write in Japanese
[Rails] How to prevent screen transition
How to use binding.pry [53 days left]
How to use Ruby on Rails
How to deploy Bootstrap on Rails
[Rails] How to speed up docker-compose
How to use setDefaultCloseOperation () of JFrame
[Rails] How to add new pages
From introduction to use of ActiveHash
Rails on Tiles (how to write)
[Rails] How to write exception handling?
[Rails] How to install ImageMagick (RMajick)
[Rails] How to install Font Awesome
[Rails] How to use Active Storage
How to introduce jQuery in Rails 6
[Rails] How to implement star rating
From introduction to usage of byebug
How to return Rails API mode to Rails
How to get along with Rails
[Introduction to Rails] form_with (local: true)