RJS Errors with Firebug Goodness

Posted by Trey Fri, 18 Jul 2008 19:02:00 GMT

I use firebug for all of my javascript debugging. It’s an excellent application, but one thing that has always bothered me is that rail’s built-in RJS exception handler shows it’s output in an alert. Firebug has the very handy console.log() function. Why can’t rails use that?

Now it can.

The code is smart enough to tell if Firebug is loaded or not and gracefully falls back to alert if it isn’t. As an added bonus you can now cut and paste the content as well as see multiple errors.

Usage

Copy the code to a file that is loaded at rails start up. RAILS_ROOT/config/initializers/rjs_debug.rb would work nicely.


module ActionView
  module Helpers
    module JavascriptHelper
      class JavaScriptGenerator
        module GeneratorMethods
          def to_s #:nodoc:
            returning javascript = @lines * $/ do
              if ActionView::Base.debug_rjs
                source = javascript.dup                                                                                                                                                                          
                javascript.replace "try {
#{source}
} catch (e) "                                                                                                                                              
                javascript << "{ var rjs_debug = alert; if(window.console && window.console.firebug) { rjs_debug = console.log }; rjs_debug.apply(this, ['RJS error:\n\n' + e.toString()]); rjs_debug.apply(this, ['#{source.gsub('\','