Baris
Baris

Reputation: 470

JavaScript var fails initializing on long text with special characters in Rails

I try to initialize a JavaScript variable in a .js.erb template with a text having new lines, and special characters like " > , " ". To visualize this text, think about the long email conversations, earlier reply lines start with char ">".

Text is fetch in the rails' controller, and initialize a ruby variable with it. So, I use the following line to complete the process, but it fails (might be cause of new line). However, I think it's very common case to have such a string values in daily web programming. How do you overcome this issue (jQuery options are welcomed)

var jstext = "<%= @rails_long_text %>";

Upvotes: 0

Views: 143

Answers (1)

apneadiving
apneadiving

Reputation: 115541

Do:

var jstext = "<%=j @rails_long_text %>";

This will escape the content to fit js expectations.

Upvotes: 2

Related Questions