Thursday, October 04, 2012

Debugging Rails Applications

Notes from Rails Cookbook. Since the book is based on Rails 1.2 only some of the recipes are still applicable to Rails 3.2.

1. Ruby syntax checker:
     $ ruby -cw test.rb
        w - warn about questionable code
        c - check syntax

       In vim you can check the syntax quickly while editing by doing:
        :w !ruby -cw

2.  Dump environment info in views:
     <%= debug(headers) %>
     <%= debug(params) %>
     <%= debug(request) %>
     <%= debug(response) %>
     <%= debug(session) %>
     <%= debug(request.env) %>

3. Use rails any_object.to_yaml in your controller's action method.

4. Filter development logs:
    logger.warn "### something went wrong: #{obj.inspect}"

    Show only messages beginning with ### :
    $ tail -f log/development.log | grep "^###"

   This tip combined with Tagged logging feature in Rails 3.2 is powerful debugging tool.

5. Use Firebug console tab to inspect the client server interaction when making AJAX calls.

6. Use Live HTTP Headers to inspect HTTP traffic.