1

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

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
Dirk
  • 3,073
  • 4
  • 31
  • 36
  • Legitimate problem, but it's a frequently asked question. You may want to browse other questions tagged [ruby] and [debugging] or [ruby-on-rails] and [debugging], and sort by votes. One that I wrote is http://stackoverflow.com/questions/3955688/how-do-i-debug-ruby-scripts , but it's more for Ruby-only apps than Rails ones. – Andrew Grimm Nov 10 '11 at 00:12

6 Answers6

4

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
  debugger
  # ... more code
end

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.

Dty
  • 12,253
  • 6
  • 43
  • 61
3

I do not start Rails project without 'pry' gem.

Add gem to Genfile:

group :development, :test do
  gem 'pry'
end

and stop request execution anywhere in your project, just put binding.pry to your model, controller, tests ..., or <% binding.pry %> in your view's, templates, partials.

Then you can check what ever you want objects, params, variables ... Type exit to leave pry environment, and request will continue.

Felix Gerber
  • 1,615
  • 3
  • 30
  • 40
Ivica Lakatoš
  • 301
  • 2
  • 3
3

i use a combination of irb, print statements, logging and pry bindings. (pry is a great gem)

irb is a great way to just play around with your ruby or rails app in the console. You could just enter the code from your controller (or similar) and see if it breaks in console for faster feedback loop. But remember you have to do reload! if you change anything in your class/module.

print statements are easy if you're running tests and just want it to output something a different points in your test. But if your testing in a browser I would recommend writing to the logger: Rails.logger.debug "...". But remember to set your logging level in your configuration to DEBUG -or- you can just do Rails.logger.info instead which should show up by default. Then you can just tail or view the logs in my_app/logs/development.rb.

My favorite method for really tricky bugs is that if the error is happening in a test you can just place binding.pry in the preceding line and then it will pause your test at that line and drop you into a console. I recommend watching the rails casts for more in-depth info: http://railscasts.com/episodes/280-pry-with-rails

bkempner
  • 652
  • 3
  • 8
  • Just in case you didnt know, Pry can be used as a full IRB alternative, just type `pry` at the command prompt instead of `irb`. – horseyguy Nov 09 '11 at 23:09
0

Beside pry gem, another option would be byebug. This gem enables you to temporarily stop code execution at a breakpoint, which is marked with keyword byebug inside the code. When execution reaches the breakpoint, a marker will be pointing to the current line, and you are able to type in commands.

This Gem offers a huge set of commands, the most commonly used ones are:

next - this command enables you to go to next line

step - goes into each invoked method step by step

break - it stops the execution of code

continue - continues code execution

This is a great article to check for debugging in rails.

Nesha Zoric
  • 6,218
  • 42
  • 34
0

The Ruby on Rails Guide would be a great place to start, but there's plenty more.

Alex
  • 9,313
  • 1
  • 39
  • 44
0

I always have a rails console session or at minimum an irb session to play with to see if things do what I think they do.

I also use RubyMine which has an excellent integrated debugger http://www.jetbrains.com/ruby/

Mark Bolusmjak
  • 23,606
  • 10
  • 74
  • 129