Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在js.erb文件中呈现文本字符串_Ruby On Rails_Erb - Fatal编程技术网

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()