Pages

Thursday, February 7, 2013

Debugging tips in Ruby on Rails

from: http://paulsturgess.co.uk/articles/39-debugging-tips-in-ruby-on-rails


Update Feb 2010 – Even though this article is now years old, these debugging tips still hold true.
There are quite a few different ways to go about analysing application errors in Ruby on Rails. I know when I first started out I found the error messages were quite cryptic.
On a typical application error there are three grey boxes. The first tells you the name of the error that ocurred.
The second shows the extracted source i.e. the line of code causing the error.
The third box tells you exactly where that line of code can be found. The key is to look past the lines beginning /ruby/lib/ruby/gems/ and look for the first one that begins with #{RAILS_ROOT}. It will tell you the exact file and line number you should be looking at.

Output to the console

Outputing to the console is useful for quickly testing to see your code is running as you expect. Literally just use:
puts "bla"

Output to the log

You can also output to the log. Sometimes outputting a variable can help. For example:
logger.info("bla: #{@object.name}")

Debug an object on the page

A useful way to check what an object contains is to output it in your view. Just use the following line:
debug @object
This will show every method available to the object wrapped in a <pre> tag.

Tail the log

Tailing the log file means that when you load the page, the console will output detailed information on every line of code that is run, including all SQL queries exactly how they are executed.
For linux/mac just open up your console, navigate to the log folder and use the following line:
tail -f development.log

Using Ruby irb

irb is a Ruby module that lets you enter Ruby programs interactively and see the results immediately.
It's great for testing code in an environment independant of your application. To use it, type the following at the command line:
script/console
You can then run any ruby you like.

Using breakpoint

Arguably one of the most under-used features in Rails and it has to be one of the best debugging tools. Using breakpoint gives you an irb prompt at the exact point in your code where you placed the breakpoint.
Simply place the word breakpoint in your code before the error. Now open the console and type:
script/breakpointer
Refresh your page with the error and in the console, after a few seconds, the prompt will be available.
Essentially you can now test your code, exactly as it is in your file. For more detailed instructions check the Rails wiki article on breakpoint

Debugging in production mode

Rails will show a 500 error page to your users when there is an application error. No application internals will be revealed and I it's probably a good idea to style this page to be as 'friendly' as possible.
However, this makes it somewhat trickier to debug issues in production mode. Which brings me onto Exception Notifier...

Exception notifications

One of the best debugging tools I have come across is the Exception notficiation plugin. Whenever a user manages to come across an aplication error you will receive an email with full details of that error. The level of detail is more than enough to help you decipher what may have happened. Install the plugin using:
ruby script/plugin install exception_notification
Check out the exception notifier read me for details on how to tell the plugin your IP so that you can also get the error printed to the screen and not in an email. This ensures you get error messages, while your users get custom, error friendly, pages.

No comments:

Post a Comment