Saturday, February 8, 2014

Debugging your Rails apps - how I go about it generally

Hi,

To debug your Rails apps the following cmds are what I generally use.

a. Rails.logger.info . They are many variants to using this based on different scenarios. One of them is -
1. To just get a better hang of the flow of how the call passes goes from one place to another-

Rails.logger.info " Your Message goes here"

An easy way of finding the same in your log could be

Case 1

Rails.logger.info "\n*********\n Your message goes here\n********\n"

2. To get the flow and also to see the values used/passed anywhere in the method(s) called as part of your action which triggered the functionality

Say if 

Case 2

a = "hi"
Rails.logger.info "\n*********\n Your message goes here - #{a}\n********\n"

The above statement will print your message with the value of a.

3. Another very useful thing that I use from time to time is making use of the 'inspect' option. With experience I can definitely say it's very helpful when you for e.g., want to see what all are passed as part of a click of a submit button, the entire list of params that are passed(with the exact format like for e.g., say your passing a hash with multiple key value pairs) or basically the object values can be clearly seen and printed via the Rails Logger.

Sample e.g.,

Case 3

Rails.logger.info "\n*********\n Your message goes here - #{a.inspect}\n********\n"

Hope you find them useful. In case you have better suggestions on can one better debug, please feel free to share your suggestions as comments to this post.

You can read more about how to better debug your Rails apps from the Rails Guides (I've taken a reference from Rails 2.3 guides just as an e.g.,).

Thank you.


No comments:

Post a Comment