views:

2584

answers:

11

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.

+4  A: 

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.

Pete TerMaat
+2  A: 

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.

csexton
+5  A: 

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.

csexton
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 :-) http://blog.nanorails.com/articles/2006/7/14/a-better-rails-debugger-ruby-debug
Abel Martin
A: 

Debuggin in rails is simple if you know how to read the error stacktrace!! But if you need to explicitly watch out the values during the runtime then u can use the rails breakpointer.Below is the link to how-to on breakpointer ..hope this helps!!!

http://destiney.com/blog/rails-breakpointer

A: 

I can't speak for Eclipse (never worked well for me) or Aptana (not tried) but from experience I can say that both NetBeans and RubyMine will do what you want. I both cases you should probably make sure that the ruby-debug-base and ruby-debug-ide gems are up-to-date: RubyMine in particular didn't work for me until that was done.

Mike Woodhouse
A: 

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.

happythenewsad
+1  A: 

Actually, I had the same problem with Aptana. Run > Debug As > Ruby Application just doesn't work. I finally made the debugger work by going to the Servers tab, and then start the server in debug mode. After that, set some breakpoints and trigger the corresponding action. Hope this helps.

misterclark
+3  A: 

maybe not relevant, but I wanted to post somewhere: got the error: "undefined method `run_init_script' for Debugger:Module" running the debugger in rails 2.3.2. Did a sudo gem install ruby-debug and the problem went away.

+1  A: 

There's a guide here for Netbeans.

ski
+1  A: 

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.

Ryan Bigg
It works .Thanks for the tip!
mykhaylo
A: 

i cant set any breakpoints, they just dont get set grgrgrgrgrgrgr

Pablo Tom