Using rails' escape_javascript outside of views

Rails provides a handy function to escape string to be used in your javascript code. Unfortunately, it is only available in your views. It is very easy though to make it available outside of views, eg in your helpers, as it suffices to require and include one module:

require 'action_view/helpers/javascript_helper'
include ActionView::Helpers::JavaScriptHelper

# escape_javascript is available here.
Syndicate content