Dirk Dirk - 5 months ago 7
Ruby Question

Ruby / Rails debugging strategy

Please can you share your approach / methodology to debugging in Ruby / Rails.

I'm busy with the Rails tutorial, and am getting the error:

NoMethodError in UsersController#show
undefined method `microposts' for #<User:0x83b43e8>

And that got me thinking about debugging strategies. Does anyone have advice for a new Rails user (and new MVC user) on strategies to approach debugging. What path do you follow? Is there a generally accepted approach? Is there a way to step through the code?

Right now I am using unit testing as a kind of "lint" checker, but that only goes so far.

Although I want to solve it, the actual error I am getting right now is not the main thrust of this question.

(PS: The problem is not a duplicated "show" as documented in elsewhere on Stackoverflow

Dty Dty

I haven't seen this mentioned yet but another option is to put a debugger statement in your code. Like this

def some_method
  something = 3
  # ... more code

If this is in a rails app when the code reaches debugger it will cause the terminal window running the web server to jump into something that looks like an irb session (I'm not exactly sure what it is). From there you can do puts something to see what the value is for example. You can even puts params to see what all the params values are. You can also step through the code, jump to a specific line, etc.

Pry seems to be a better way to go about this but it's how I used to debug before I knew about pry.

Note: You might need to do require 'ruby-debug' if you're doing this outside of rails.