Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Rails debug cheat sheet


Replace IRB with Pry (in your Gemfile) and Byebug with pry-byebug.

gem 'pry-rails', group: [:development, :test]
gem 'pry-byebug', group: [:development, :test]

Using PRY

Put pry somewhere in your code to start pry session

def my_method

Common commands

  • Show method source
pry> $ object.method
# or class instance method
pry> $ Class#method  
# or class static method
pry> $ Class.method
  • Show method docs
pry> ? object.method
# or class instance method
pry> ? Class#method  
# or class static method
pry> ? Class.method
  • Show object methods and instance variables
pry> ls object
# or only public methods
pry> ls -m object
# or grep by name
pry> ls -G has_ object
# or instance variables
pry> ls -i object
  • Move between objects
pry> cd user
pry(User):1> self
=> User
# switch back
pry(User):1> cd ..
# or switch to the top
pry(User):1> cd /
  • Stop PRY session
# if you're in the top scope
pry> exit
# otherwise
pry> exit-all
# or
pry> !!!

Rails specific commands

  • Show routes
pry> show-routes
  • Show models info
pry> show-models
pry> show-model User
# or search model by attributes
pry> show-models -G password
  • Show controller routes
pry> find-routes UsersController

Debugging with pry-byebug

  • Start debug session
# somewhere in your code
  • Navigation
# step into the next line/method
pry> step
# or step multiple times
pry> step 3

# step over to the next line/method
pry> next
# or
pry> next 2

# exit current stack frame
pry> finish

# move the stack frame up/down 
pry> up
pry> down
pry> up 3
pry> down 4

# continue to the next breakpoint
pry> continue
# or
pry> exit
  • Breakpoints
# add breakpoint by the line number
pry> break /path/to/project/app/models/user.rb:22
# or by class and method name
pry> break User#admin?

# disable all breakpoints
pry> break --disable-all
# disable by number
pry> break -d 1
# enable by number
pry> break -e 1

# breakpoint with condition
pry> break User#my_method --condition 4 id == 2 # breaks only if id equals 2

Capybara helpers

  • Save page as html
# Change output dir
Capybara.save_and_open_page_path = "./tmp/capybara"

# Add to your code to save current page state
# or save and open saved page in your browser

How to open saved page when using VM.

  • Save page as image
  • Pause test server and login to test session
# Setup Capybara server (somewhere in acceptance_helper)
Capybara.server_port = 3000 # or everything you want)
Capybara.server_host = "" # to access server running within VM

# in your test code
pry # simply start PRY session

# and then open browser and login with current user credentials

Debugging js views

# view.erb.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment