Jerome
Jerome

Reputation: 6217

rails text_area format for javascript

An attribute of type text needs to be generated within a javascript.

Some records have carriage returns inserted by the user and these need to be passed to javascript in form \n. However when I place

<%= uc.main_text %>

within the javascript, it returns carriage returns

Salties at bottom...

what happens here I am not certain

when \n is needed.

Salties at bottom...\n\nwhat happens here I am not certain

From the console calling the attribute returns a proper string

uc.main_text
[...] \r\n\r\n [...]

How can this be transposed for use by the javascript?

Upvotes: 0

Views: 46

Answers (2)

TolichP
TolichP

Reputation: 134

You can try String.raw(), if I understood your question right.

Upvotes: 0

fphilipe
fphilipe

Reputation: 10054

You need #escape_javascript:

string = "Salties at bottom...

what \"happens\" here I 'am' not certain"
puts %{var string = "#{escape_javascript(string)}"}

This will print:

var string = "Salties at bottom...\n\nwhat \"happens\" here I \'am\' not certain"

Upvotes: 1

Related Questions