0

I have a custom error proc that gets initialised in application.rb:

config.action_view.field_error_proc = Proc.new do |html_tag, object|
  error_msg = object.error_message.join(", ")
  "<span class='field_with_errors' data-error='#{error_msg}'>#{html_tag}</span>".html_safe
end

I would like to escape javascript, so I modify it:

config.action_view.field_error_proc = Proc.new do |html_tag, object|
  error_msg = object.error_message.join(", ")
  "<span class='field_with_errors' data-error='#{escape_javascript(error_msg)}'>#{html_tag}</span>".html_safe
end

However I get the following error:

undefined method `escape_javascript' for #<MyApp::Application:0x00000006edd2e8>

I'm using rails 3.0

Any tips would be greatly appreciated.

1 Answer 1

3

You have to include the helper in order for it to be available. This should work:

include ActionView::Helpers::JavaScriptHelper

config.action_view.field_error_proc = Proc.new do |html_tag, object|
  error_msg = object.error_message.join(", ")
  "<span class='field_with_errors' data-error='#{escape_javascript(error_msg)}'>#{html_tag}</span>".html_safe
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.