Rails - 转义空格和引号以在 javascript 中使用
我正在尝试在 ajax 调用中执行以下操作
index.js.erb
$("#dialog").text("<%= render("index") %>")
它告诉我在我的 js 控制台中存在未终止的字符串文字(这是真的,因为部分中有引号和空格。
我尝试使用 escape_javascript 但然后它显示部分代码。我也尝试调用 raw(escape_javascript(render("index")) 但没有成功。
我认为我需要做的就是转义双引号并删除空格。是否有一个函数可以实现此目的干杯
蒂姆
I am trying to do the following in an ajax call
index.js.erb
$("#dialog").text("<%= render("index") %>")
It tells me in my js console that there's an unterminated string literal (which is true because there are quotes and whitespace in the partial.
I have tried to use escape_javascript but it then displays the partial code. I have also tried calling raw(escape_javascript(render("index")) with no success.
I think all I need to do is escape the double quotes and remove the whitespace. Is there a function for this?
Cheers
Tim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,问题不是转义,而是 jQuery 文本方法使用的自动编码。
我使用
“很难找到”是因为我的 Firebug 对我撒谎,并向我显示渲染的字符而不是编码的版本。
Ok the problem was not the escaping but the automatic encoding that the jQuery text method was using.
I used
Hard to find because my Firebug was lying to me and showing me the rendered characters not the encoded versions.