[英]Returning javascript from rails helper
I am having problems returning javascript from helper functions. 我在从辅助函数返回javascript时遇到问题。 I am unable to get the following test case to work: 我无法使用以下测试用例:
function in helpers/application_helper.rb helpers / application_helper.rb中的功能
def show_stuff
return '$("div#flash").html("<p> stuff </p>");'
end
now I try to call this helper function in general.js.erb 现在我尝试在general.js.erb中调用此辅助函数
<%= show_stuff %>
Here is the output 这是输出
$("div#flash").html("<p> stuff </p>");
I've also tried show_stuff.html_safe and raw show_stuff and had no luck. 我也尝试过show_stuff.html_safe和raw show_stuff,但没有运气。 I feel the issue is Rails auto-escaping html but have been unable to find a solution. 我觉得这个问题是Rails的自动转义HTML,但一直未能找到解决办法。
ERB is escaping the characters in your outputted Javascript. ERB正在转义输出的Javascript中的字符。
To prevent this happening use the raw
method in your template. 为防止这种情况发生,请在模板中使用raw
方法。
<%= javascript_tag do %>
<%= raw(show_stuff) %>
<% end %>
I've added the javascript_tag
method around this for context. 我为此添加了javascript_tag
方法作为上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.