Skip to content

escape_javascript and line breaks #5194

fespinoza opened this Issue Feb 27, 2012 · 4 comments

4 participants


I found this issue in the app lighthouse, but I don't know if it solved or not...but still occurs in rails 3.0.8. Thanks

lest commented Feb 27, 2012

Could you please provide an example of this issue?


yeah, sorry I was so little explanatory for that...

I was using a index.js.erb view for my controller action,

here the code from my view

enter image description here

then the code is called from a javascript ajax request, here is the response

enter image description here

also I copied and pasted the code into the chrome console and here is the output

enter image description here

then I chained a method call to render I my view was like

enter image description here

this time the response was

// usuarios/ejemplo means users/example in spanish
$("#special_div").append("\u003Ch1\u003EHello World\u003C/h1\u003E\u003Cp\u003E  this is a haml file  \u003Cbr\u003E  with line breaks\u003C/p\u003E\u003Cp\u003E  and I will fail with the  \u003Cstrong\u003Eespcape_javascript\u003C/strong\u003E  function.\u003C/p\u003E");

and the console output of that was

enter image description here

that finally worked... the solution was to append a gsub call to remove line breaks, I think that doesn't have to happen with the escape_javascript function... as I googled for that I found this ticket in lighthouse

so that's all

Ruby on Rails member

@fespinoza hey, I see you're using Rails 3.0.8 right? I'm quite sure the j helper that you're using was added only in 3.1.

You can see the message in the changelog:

Make JavaScriptHelper#j() an alias for JavaScriptHelper#escape_javascript() -- 
note this then supersedes the Object#j() method that the JSON gem adds within 
templates using the JavaScriptHelper DHH

It seems you're ending up using the Object#j method, unfortunately (probably the json gem is part of your bundle). Try using escape_javascript instead to see if it works for you.


Is this still an issue?

@fespinoza fespinoza closed this May 6, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.