Ruby In Steel 1

Back to -Tutorials -Sapphire (Ruby In Steel) Tutorials -Archives -Ruby In Steel 1

How To Debug Ruby Inside ERb/RHTML

A Quick Guide and Screencast to RHTML debugging

Watch the screencast

When you are developing Rails applications you really want to be able to debug code embedded in ERb (or RHTML) templates as well as the code in ‘pure Ruby’ files such as controllers. Well, that’s no problem. Ruby in Steel lets you put breakpoints on embedded Ruby code in exactly the same way as on standard Ruby code. When you hit that breakpoint, you can trace into code and drill down into objects in the usual way. Here’s a short example using a simple Blog application.

First, I set some breakpoints in standard Ruby code such as the controller, blog_controller.rb...

Now, I also set breakpoints in view templates (HTML with embedded Ruby code) such as show.rhtml...

And even in a ‘partial’ (a chunk of reusable RHTML) such as the one that defines my Post form, _form.rhtml...

Now I press F5 to start the debugger. I load up the integrated web browser (CTRL+W, W) and log onto my blog application by entering its address: http://localhost:3000/blog/. I’ve already added some posts and comments. I’ll try leaving another comment. To do so, I click the link to the first post in order to load up the Post and Comment entry page (an HTML page which is constructed at run time from the show.rhtml view)....

This takes me to the breakpoint in the controller. Here I can use the various debugging windows to view the call stack, locals, autos and watch variables. Or I can just hover over a variable into the editor and expand the debug tip to ‘drill down’ into it...

I could press F10 or F11 to trace into or over the Ruby code that follows - however, this would take me right inside the Rails libraries, which is more detail than I need right now, so I just press F5 to continue debugging. This takes me to my next breakpoint in the view, show.rhtml. All the standard debugging features, including ‘hover and drilldown’ are here too. I check some variables and press F5 again to continue...

Now I decided to create a new post. In the web browser I click the New post link...

I hit the breakpoint in _form.rhtml partial. Once again, all the debugging features are available. This time, as a change from hover-and-drilldown, I decide to use the docked Locals window....

And so on. In short, the fact that Ruby is embedded into HTML is of no consequence to Ruby in Steel. as far as the debugger is concerned, Ruby code is Ruby code and, if it can be executed then it can also be debugged!

You can view a short screencast to show RHTML debugging in action by clicking HERE. There are more screencasts on debugging and other features of Ruby In Steel on The Movies Page.

Bookmark and Share   Keywords:  ide  movies
© SapphireSteel Software 2014