HowToDebugYourRailsApplication

How to debug your Rails Application using Netbeans 6.0.1

Coming from a Java background I loved using the debugger in Eclipse and Netbeans, because it’s very good to handle and lists every information I need at the place where I set the breakpoint, I can set new breakpoints on the fly, click through the code step by step and much more. Debugging in Rails can be very helpful to control the information that is stored in the session or the parameters that are stored in the request after submitting a form, it can be useful to debug the model functions, etc. Netbeans, since Version 6 is equipped with a Rails IDE and Debugging functionality and is a really good starting point for Beginners, because it offers Code-Completion, Tips on Ruby Code and comes with helpful functionality like a wizard to generate model classes, controllers, migration, etc.

  1. You need to have Ruby and Rails installed
  2. You need a MySQL Server with a user, normally after installation you can use root without a password.
  3. Download and install Netbeans 6.0.1 (Click the Download button in the column where Ruby is written) from Netbeans.org

To peak into the Debugging functionality just download Netbeans 6.0.1 (6.1 is still RC2 and not ready yet) and tell Netbeans in the config where your ruby files are. Therefore go to Settings/Options depending whether you use osx or windows. Click on the Ruby Icon and specify the location of your ruby interpreter.

File:370

On my machine the FastDebugger we need is already installed, if you haven’t done that already go to your command line and type

gem install ruby-debug-ide

Now we are ready to create a small Rails application and use the debugger. In Netbeans go to File — New Project, Select Ruby and Ruby on Rails Application and click next. Let’s give it the name “Rails Blog”, check that Database mysql is selected and then click finish.

File:370

After the project is created it will be visible on the left side under the Tab Project. The Directory Structure might look slightly different from a normal Rails project, but when clicking on the Files Tab you will see that it was only restructured virtually to make it easier to navigate in the folders, the original file structure is still there and you could use this project completely without Netbeans, if you like.

File:370

To be on the secure side let’s change the database.yml file and remove “socket: /tmp/mysql.sock”. At the same place add “host: localhost”. Let’s get going and create a small scaffold we can use. Right-Click on the project name (RailsBlog) and click on “generate”. Select “Scaffold” in the dropdown list and type “blog title:string body:text published:boolean”. This will create all necessary files, just click “ok”.

File:370

We are very close to debugging our little application, we still need to create the database and do the migration. Again Right-Click on the Project Name, now goto Run Rake Task — db — create.

File:370

Again Right-Click on the Project Name, now goto Run Rake Task — db — migrate. Netbeans has an Output Window where you can see all Tasks it is performing and if there were any failures.

File:370

You can’t see that window? Go To the “window” menü at the top of Netbeans and select “Output”. Later, if you don’t see the Debugging Window, you will find it there, as well. Time to Debug! I would like to see what’s happening when I type in a new Blog post an then hit the create button. To see all the parameters and session information we will set a breakpoint in the “blogs_controller” in the “create” method on line 45 that begins with “respond_to”.

File:370


To see what will happen, right-click on the project name and select debug. The Server (Mongrel or WebRick) will start in debug mode. Open your browser and go to http://localhost:3000/blogs - click on “new blog” and fill out the form. Then hit the “create” button. After you have done that 2 things will happen, the browser will wait on the answer of the request and meanwhile the processing of this request has stopped at our breakpoint, marking the line green.

File:370

A new window is open, with some variables inside,

File:370

let’s double click on the Tab to enlarge the window an get a better view. Take a look at the “BlogsController” (self) Object, open it. You will find the complete request, the session information, the blog object that was created in the “create” method and a lot more.

File:370

File:370

Double Click on the “Local Variables” Tab again to minimize the window and get back to the “blogs_controller” code.

File:12_play_step_HowToDebugYourRailsApplication.png

You can use the Arrow on the picture above to step to the next line or click on the green button to let the process finish. If you have a breakpoint later somewhere in the code, the green play button will proceed to this place. The Local variables Window will update the information while you are stepping through the code. Hint: If you use methods offered by the Rails Framework like respond_to it may happen, that stepping through the code with the arrow will open up classes from the Rails Framework. This will happen here when you try to step to the next line, because respond_to will perform some internal steps to render the result.

If you have any questions, don’t hesitate, just leave a comment on my Site, I have this Rails Tutorial there, as well.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo