views:

308

answers:

5

I'm new to Rails.

Javascript errors to an alert box injure my soul. Is there a way to send it to console.log() instead of alert()?

A: 

Yes, console.log("error message goes here")

Ryan Bigg
i think he meant the alerts, that rjs generates ;)
tliff
Yes, Rails does a lot of stuff on its own, I suppose I should add the tag rjs
rpflo
+1  A: 

for rjs, try out http://maintainable.com/software/firebug_rjs_errors (somehow the link does not work when embedded)

tliff
A: 

It depends on the Javascript error. Since JavaScript is interpreted by the client, you would need to display the error messages client-side. The only real way to log errors in the server log file is if the error is happening on the server side before the Javascript is interpreted by the client.

mdinstuhl
You're absolutely right, but console.log is a javascript function for browser debugging tools (webkit's inspector, firebug, and IE8's new thing) that I'm trying to use instead of Rails default alert().
rpflo
+2  A: 

You could override window.alert:

var oldAlert = window.alert; // reference to the original window.alert

window.alert = function(message) { 
  if (window.console && console.log) { 
    console.log(message); 
  } else { 
    oldAlert(message); // if console.log doesn't exist call window alert
  } 
}
CMS
That would work but that's quite brutal imho. But still good solution so +1
marcgg
Solution for now! But I really want to explore that plugin from below--just in case I need to use alert in the app (which I never do, I consider popups in nearly any form profanity)
rpflo
if you use the :confirm parameter in links, it will also use alert
tliff
In JS confirm and alert are different, though they are both agitating.
rpflo
A: 

You could try turning off RJS debugging of in your environment.rb file:

config.action_view.debug_rjs = false

This should turn off the alerts altogether. Another option would be overriding ActionView::Helpers::GeneratorMethods#to_s:

module ActionView
  module Helpers
    module GeneratorMethods
      def to_s #:nodoc:
        returning javascript = @lines * $/ do
          if ActionView::Base.debug_rjs
            source = javascript.dup
            javascript.replace "try {\n#{source}\n} catch (e) "
            javascript << "{ console.log('RJS error:\\n\\n' + e.toString()); console.log('#{source.gsub('\\','\0\0').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }}'); throw e }"
          end
        end
      end
    end
  end
end

I admit I'm out of my depth as far as overriding the method goes, and I don't know if it's a recommended practice. I haven't needed to do anything like this in my projects.

My personal preference would be to skip RJS and go with unobtrusive jQuery.

Andy Gaskell
Not sure how prototype is more obtrusive ... but anyway, I'm going to give prototype/scriptaculous a shot. Most the stuff I do is xhr heavy, so I'd like to "keep it in the family" instead of messing around with JS separately. I'm a mootools man myself and can already see myself wanting to use it.
rpflo
I'm sure you can go unobtrusive with prototype but that would mean giving up using a lot of the helpers AFAIK. I like writing and controlling my JS so it works for me.
Andy Gaskell