Ruby on rails 如何在js.erb文件中呈现文本字符串
为什么会正确呈现(来自Ruby on rails 如何在js.erb文件中呈现文本字符串,ruby-on-rails,erb,Ruby On Rails,Erb,为什么会正确呈现(来自example.js.erb文件): alert() 但这些都不是: alert(<%= "hello" %>) alert(<%= p "hello" %>) alert(<%= j "hello" %>) alert("<%= "hello" %>") alert(<%= hello.inspect %>) alert(<%= JSON.dump("hello") %>) alert() 警报
example.js.erb
文件):
alert()
但这些都不是:
alert(<%= "hello" %>)
alert(<%= p "hello" %>)
alert(<%= j "hello" %>)
alert("<%= "hello" %>")
alert(<%= hello.inspect %>)
alert(<%= JSON.dump("hello") %>)
alert()
警报()
警报()
?
当我查看控制台时,后者将文本字符串返回到jquery,而不使用引号,我认为这是问题的根源
但是如何修复它呢?第一个会变成
警报(123)
,它会显示123
。
其余部分将呈现alert(hello)
,这将打印变量hello
的内容,可能是空的。您想要的是其中一个:
alert(<%= "hello" %>)
alert(<%= p "hello" %>)
alert(<%= j "hello" %>)
alert("<%= "hello" %>")
alert(<%= hello.inspect %>)
alert(<%= JSON.dump("hello") %>)
警报(“”)
警报()
警报()
(第三个需要一些提示)第一个会变成
警报(123)
,它会显示123
。
其余部分将呈现alert(hello)
,这将打印变量hello
的内容,可能是空的。您想要的是其中一个:
alert(<%= "hello" %>)
alert(<%= p "hello" %>)
alert(<%= j "hello" %>)
alert("<%= "hello" %>")
alert(<%= hello.inspect %>)
alert(<%= JSON.dump("hello") %>)
警报(“”)
警报()
警报()
(第三个需要一些要求)另外,这个更安全:
alert(<% hello&.inspect&.html_safe %>)
alert()
此外,这一款更安全:
alert(<% hello&.inspect&.html_safe %>)
alert()