How do you debug a Rails application?

11,418

Solution 1

The debugger I use the most is the ruby-debug gem, which is a gdb-esque command line debugger. Once you learn a few commands it is very quick and effective, and provides you with some handy features like being able to fire up irb in the context of your program and make on-the-fly changes.

And being command line based it comes in handy when you need to debug a on a remote server.

Solution 2

You can expect more. I have used Aptana's RadRails version of Eclipse to debug a Rails app as you describe--setting breakpoints and stepping through the code.

You may be doing something wrong. It looks as if it is trying to debug an individual controller file, rather than debugging the Rails app. When I try to execute a controller file from the command line, I get a similar message:

C:\workspace\myapp\app\controllers>ruby users_controller.rb
users_controller.rb:1: uninitialized constant ApplicationController (NameError)

In Aptana RadRails, I choose Run > Debug As > Ruby Application to debug the app.

Solution 3

For the vim users I strongly recomend looking into the vim-ruby-debugger, which fits in great with Tim Pope's rails.vim scripts.

It gives you a handy :Rdebugger command, allows you to set breakpoints and open a split window to display variable values.

Solution 4

Debugging? That's just knowing where to look in the case of Ruby (and by extension, Rails) most of the time.

The problem in this case is that you probably still have your ApplicationController called application.rb where it should be renamed to application_controller.rb.

Solution 5

I'd recommend just setting up breakpoints (I actually just puts to console) for 99% of debugging with RoR - this method is simple and usable across any IDE, so you never need to learn how a new debugger works.

Share:
11,418
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin about 2 years

    Is it possible to debug a Rails application in a similar way to a Java application - setting breakpoints and stepping into the code?

    What are the best tools for this?

    I have a hybrid Java/Ruby on Rails application which I can run in Eclipse or Netbeans.

    I would like to step into some code in this app and try to figure out the cause of a problem I'm having.

    In Eclipse if I set a breakpoint in my blog_controller and then choose the 'Debug' button, it seems to use the ruby-debug-ide gem to execute the code but I get this unhelpful output and no option to step into any source:

    Fast Debugger (ruby-debug-ide 0.4.5) listens on localhost:56726
        ./war/WEB-INF/app/controllers/blog_controller.rb:1
        C:/Ruby18/lib/ruby/gems/1.8/gems/ruby-debug-ide-0.4.5/lib/ruby-debug.rb:101:in `debug_load'
        C:/Ruby18/lib/ruby/gems/1.8/gems/ruby-debug-ide-0.4.5/lib/ruby-debug.rb:101:in `debug_program'
        C:/Ruby18/lib/ruby/gems/1.8/gems/ruby-debug-ide-0.4.5/bin/rdebug-ide:82
        C:/Ruby18/bin/rdebug-ide:19:in `load'
        C:/Ruby18/bin/rdebug-ide:19
    Uncaught exception: uninitialized constant ApplicationController
    

    I'm not sure if I'm doing something wrong or if this is all I can expect.

  • Abel
    Abel about 15 years
    This is the method that I go with and I really like it. Here's a link that features the debugger command in black, easy to read text as opposed to light gray :-) blog.nanorails.com/articles/2006/7/14/…
  • Pedro Rolo
    Pedro Rolo over 13 years
    This is a must-know! There are plenty of situations where an IDE may not be around! Here's a guide: guides.rubyonrails.org/debugging_rails_applications.html