首页 > 解决方案 > 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?

标签: javascriptruby-on-rails

解决方案


你需要#escape_javascript

string = "Salties at bottom...

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

这将打印:

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

推荐阅读