How to Debug in Rails

Every developers need debuggers now and then. I have been using ruby-debug for a while now.

You can check whether you have ruby-debug installed:

gem list ruby-debug

If you do not have ruby-debug installed, you can run the following command:

gem install ruby-debug

I have been using Mongrel for my development. To enable debugging, you should start your server with –debug option:

ruby script/server –debug

To stop at a certain point in your code, all you need to do is to add a ‘debugger’ line:

  def edit
    @task = Task.find(params[:id])
    debugger
    current_user_id = find_current_user_id()
  end

Once your application reach the ‘debugger’ line, the server window will turn into:

(rdb:10)

To check for a variable value, type:

pp @task

You can explore other commands easily by typing ‘help’.

(rdb:10) help
ruby-debug help v0.10.1
Type ‘help <command-name>’ for help on a specific command


Available commands:
backtrace  delete   enable  help    next  quit     show    trace
break      disable  eval    info    p     reload   source  undisplay
.
.

Happy debugging! :)

Leave a Reply