4

Is there a way like how we debug models / controllers with logger.debug? Or even a better method?

Thank you!

Edit 1

Using ruby-debug seems like a steep learning curve for me, could anyone point me something similar to logger.debug, perhaps?

Edit 2

Alright, I think I started to get a grasp on ruby-debug.

Some useful notes for newbies to setup & use ruby-debug:

gem install ruby-debug
in config/environments/development.rb add

include 'ruby-debug'

then just above the code you want to debug add:

debugger

if you need to debug third party plugin / gem, use

include 'ruby-debug'
debugger

just use ruby script/server to run, no need to add --debugger

Edit 3

This plugin really helps me out in understanding the flow of Rails application. I highly recommend this to any newbies going pro!

Simply setup ruby-debug, then put debugger code anywhere in your controller under the action your application requested. You will then find great revelation!

rstackhouse
  • 2,238
  • 24
  • 28
jaycode
  • 2,926
  • 5
  • 35
  • 71
  • Yea, using a command-line debugger is definitely a learning process, and ruby-debug is certainly no Visual Studio/XCode/etc for debugging (of course, those tools do not work with Ruby--although I bet Radrails has a debugger), but it's a huge plus over printing/logging values. – wuputah Aug 29 '10 at 00:30

1 Answers1

4

I highly recommend you learn to use ruby-debug. You can install it by doing:

gem install ruby-debug

You can then add a debugger statement to your code either in the plugin code or where your code calls the plugin, step through it, and see what's going wrong.

I also personally use these settings which makes it a bit easier to use - put them in a ~/.rdebugrc file.

set autoeval
set autolist
set autoreload
wuputah
  • 11,285
  • 1
  • 43
  • 60
  • I tried using it for a set of test script inside the plugin (by running ctrl + R in Textmate), it returned NoMethodError: undefined method `logger' for true:TrueClass what should I do now? – jaycode Aug 27 '10 at 16:28
  • You can't use `Cmd+R` from Textmate with `rdebug` - using the debugger is interactive, so you need an interactive prompt. Run your test script in Terminal with the `rdebug` command. You can also run it with plain ruby if you `require 'ruby-debug'` (rubygems will also need to be loaded for that to work). – wuputah Aug 27 '10 at 17:19
  • Here's a screencast which covers using ruby-debug in a Rails app: http://railscasts.com/episodes/54-debugging-with-ruby-debug – wuputah Aug 27 '10 at 17:20