#Debugging Rails
It is a capital mistake to theorise before one has data. Insensibly one begins to twist facts to suit theories, instead of theories to suit facts.
— A Scandal in Bohemia, 1892
##Steps for debugging
- Hypothesis - read the exception messages properly!
- Analysis - think what the program is doing, rather than what it isn't doing. What do you want it to do?
- Experimentation - see below
- Conclusion
If you get stuck, take a break, switch your mind off, then start from the beginning again.
- Always think like the interpreter. It's very rational
- The problem is likely to be in your variables so dissect them
Dissection tools
These are mostly summed up in this article from the Rails docs which is highly recommended going over when you have the time.
Remember the choice of your tool is personal and changes from person to person but if you're not using any of these tools you're doing something wrong!
- Print statements - remember if you do this in the controller it will only show up in the server output
- Raise exceptions
- Rails logger -
tail -f log/development.logshows the log - Pry debugger - add
pry-railsto your Gemfile, then usebinding.pryto break the code,exitto continue - Rails console/irb
- Git log/diff - what's changed since the bug was introduced?
Some further reading:
- Zen of debugging
- The psychology of debugging
- Debugging Railscasts - some of these are subscription only