如何使用 Rails 将多行字符串插入到 coffeescript/jquery 值中
How to insert multi line string into coffeescript/jquery value with Rails
我的 Rails 项目中有一些 CoffeeScript,我正试图用它来更新文本区域。我的 CoffeeScript 是:
$('#video_description').val(<%= @description %>")
returned 文本是来自 YT gem 的字符串(来自我的一个 YouTube 视频的描述)。一个例子可以是:
Testing that this works
Does this work?
当我加载页面并使用 Chrome 中的开发人员工具检查它时,CoffeeScript 看起来像:
$('#video_description').val("Testing that this works
Does this work?")
新行避免了 CoffeeScript 对解释器有意义,并引发了以下消息:
Uncaught SyntaxError: Invalid or unexpected token
我试过将 CoffeeScript 替换为:
$('#video_description').val(<%= h @description %>")
这没有任何效果(除了转义实际字符串中的所有单引号)。这与字符串按原样放入引号有关。
我需要让文本字符串具有 \n
而不是回车符 return。
任何解决这个问题的帮助都会很棒。
使用escape_javascript()
方法(快捷方式:j()
)
$('#video_description').val("<%= j(@description) %>");
我的 Rails 项目中有一些 CoffeeScript,我正试图用它来更新文本区域。我的 CoffeeScript 是:
$('#video_description').val(<%= @description %>")
returned 文本是来自 YT gem 的字符串(来自我的一个 YouTube 视频的描述)。一个例子可以是:
Testing that this works
Does this work?
当我加载页面并使用 Chrome 中的开发人员工具检查它时,CoffeeScript 看起来像:
$('#video_description').val("Testing that this works
Does this work?")
新行避免了 CoffeeScript 对解释器有意义,并引发了以下消息:
Uncaught SyntaxError: Invalid or unexpected token
我试过将 CoffeeScript 替换为:
$('#video_description').val(<%= h @description %>")
这没有任何效果(除了转义实际字符串中的所有单引号)。这与字符串按原样放入引号有关。
我需要让文本字符串具有 \n
而不是回车符 return。
任何解决这个问题的帮助都会很棒。
使用escape_javascript()
方法(快捷方式:j()
)
$('#video_description').val("<%= j(@description) %>");