introduction to

Debugging in Rails

@deniseyu21

WHAT IS DEBUGGING?

The process of identifying and removing errors from computer programs.

DEBUGGING, at a glance

  1. Read the stack trace
  2. Use the console
  3. Use third-party gems

BUT FIRST...

  1. What am I trying to accomplish?
  2. Where is the error? (routes? database? migrations? configs? dependencies? etc.)
  3. Can I change the error?

Ensure method names are correct, and consider nil object cases

READING THE stack trace

Use 'P' to inspect server-side

app/controllers/listings_controller.rb
Rails server log:

.Inspect things client-side

Using the view to debug is a common strategy. (Just remember to remove it before pushing...)

To the console!

(type "rails console" or "rails c" into your shell)

BINDING.PRY IS YOUR FRIEND

Insert 'binding.pry' anywhere in your models, views, or controllers. Your Rails server will suspend and you can look around your application at that stage.

 

This requires the gem 'pry-rails'.

RECOMMENDED GEMS

  • Better Errors (github.com/charliesome/better_errors)
     
  • Byebug (github.com/deivid-rodriguez/byebug)
     
  • Pry-Rails (github.com/rweng/pry-rails)

 

FURTHER READING

  • Rails 4 official debugging guide: 
    guides.rubyonrails.org/debugging_rails_applications.html
  • RailsCasts on stack traces: 
    railscasts.com/episodes/24-the-stack-trace
  • http://nofail.de/2013/10/debugging-rails-applications-in-development/

Rails Debugging

By Denise Yu

Rails Debugging

  • 1,890