I'm a software engineer. You can usually find me at the local pub, bouldering, or hunting for the best Korean fried chicken in London.
Debugging in Rails
WHAT IS DEBUGGING?
The process of identifying and removing errors from computer programs.
DEBUGGING, at a glance
- Read the stack trace
- Use the console
- Use third-party gems
- What am I trying to accomplish?
- Where is the error? (routes? database? migrations? configs? dependencies? etc.)
- 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
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'.
- Better Errors (github.com/charliesome/better_errors)
- Byebug (github.com/deivid-rodriguez/byebug)
- Pry-Rails (github.com/rweng/pry-rails)
- Rails 4 official debugging guide:
- RailsCasts on stack traces:
By Denise Yu