Tips for debugging RJS code

rjs.png

I recently upgraded co.mments to use Rails 1.1.2. One of my favorite new features is RJS.

RJS is great for writing multi-action buttons. The code is a blend of Ruby simplicity, but can do anything JavaScript can. It lives in the Ruby action or associated RJS view. You write actions that the browser executes, instead of sending data to the browser and trying to interpret it there.

Debugging with AJAX is harder than plain HTML. When you make an HTML request and get an error, it shows up in the browser. When you make an AJAX request and get an error, most of the time nothing interesting happens. But it doesn’t have to be painful if you use the right combination of tools.

Here are a few tips I collected for debugging RJS code.

  • To RJS or not RJS. RJS is great when the user clicks a button and a lot of stuff happen on the page. It’s an overkill for updating one piece of content. Using Rails to return a piece of HTML and prototype.js to update is easier, and you get a UI that is also an API. My rule of thumb: only RJS if clicking a button causes more than one action.
  • Remember the page. Don’t forget that RJS relies on an object, most examples use the variable page. Use alert(’done’) with JavaScript but page.alert(’done’) with RJS. It takes a few trials to make a habit out of that.
  • One eye on the console. I run the server as a separate command line window, showing requests and errors as they happen. It’s easier than reloading the log file in a text editor. Half my bugs are errors in Ruby code that show up as stack traces in the console. That’s the first place I look at when something goes wrong.
  • The other eye on RJS debug. RJS wraps code with try/catch, so JavaScript exceptions don’t show up in the console. This one is easily solved by turning RJS debugging on. It pops alerts in response to JavaScript exceptions. Put config.action_view.debug_rjs = true in your development environment configuration.
  • FireBug knows all. Then there’s code that doesn’t throw exceptions, but doesn’t do what you expect it to. That’s when I turn to FireBug using the Show XMLHttpRequests option. I use it to find XHR requests that send the wrong parameters, or responses that execute the wrong JavaScript code.
  • View live source. Using View Source with AJAX is like driving with blinds on. It shows the page loaded by the browser but not any changes since. FireBug and Web Developer both have a DOM inspector, but they use trees which require a handful of clicks before you can find what you want. It’s quicker to navigate with View Source Chart using the page up/down keys.
  • XHR 404/500. Requests that hit the wrong controller/action, or cause an exception in the Rails code, result in HTML pages that do nothing interesting on the client. Non-responsive AJAX can be annoying. I found an easy solution for that by forcing 404/500 errors to redirect to an error page using page.redirect_to.

6 thoughts on “Tips for debugging RJS code

  1. Pingback: 16 RJS Resources and Tutorials for Rails Programmers

  2. Pingback: my life » RJS Template tutorials with Ruby on Rails

  3. Can you give a few more details how you actually use FireBug to trace Show XMLHttpRequests?

    Your site was most helpful and thanks for the great tips on View Source Chart and FireBug

  4. David,

    The FireBug console will show every XHR request made from the browser, so you can look at the query string to make sure it’s making the right request to the server, and the result to making sure it’s sending the proper response.

    When you do View Source in Firefox, Firefox shows only the original HTML page that it loaded, but not any changes made with JavaScript (or RJS). Instead I use View Source Chart, which shows the HTML code after JavaScript modifications.

    Some bugs are changes that replace the wrong element, or don’t change an element to be visible, etc, which you can only find by looking at the HTML source.

    Also, if you know which element you’re looking for, you can also do something like inspect($(‘sidebar’)) in FireBug.

  5. The View Source Chart is pretty slick. Here’s another tip. It won’t always work for debugging, but in a pinch, you can select a bit of text in the browser, right-click and select View Selection Source – you don’t see the whole page, but what you do see is the in-memory DOM.

  6. Pingback: 16 RJS Resources and Tutorials for Rails Programmers « Mystitech’s Weblog

Comments are closed.