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()?
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()?
for rjs, try out http://maintainable.com/software/firebug_rjs_errors (somehow the link does not work when embedded)
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.
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
}
}
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.