我有一个包含两个模板的网站
一个模板句柄定期接收闪存消息。另一个模板需要通过 javascript 触发的消息:
var notyfy = notyfy(
{
text: 'Hello I am an error',
type: 'error' // alert|error|success
});
我已经建立了一个flash_helper
. 此助手监视哪个模板处于活动状态,并应发送回正确的“闪存消息”:
- 我不知道如何在 view_helper 中发送 javascript
这是我的 flash_helper.rb
# called via <%= flash_messages %>
module FlashHelper
def flash_messages
return if flash.empty?
if controller.send(:_layout) == "pro/application"
flash.collect do |type, message|
# HERE SHOULD THE MAGICK HAPPEN :-)
javascript_tag "alert('All is good')", defer: 'defer'
end
else
flash.collect do |type, message|
content_tag(:div, :class => "notification-box notification-box-#{type}") do
content_tag(:p) do
content_tag(:i, nil, class: "icon-ok") +
message
end +
link_to("#", class: "notification-close notification-close-#{type}") do
content_tag(:i, nil, class: "icon-remove")
end
end
end.join("\n").html_safe
end
end
end
我的结果始终是纯文本:
["<script defer=\"defer\" type=\"text/javascript\">\n//<![CDATA[\nalert('All is good')\n//]]>\n</script>", "<script defer=\"defer\" type=\"text/javascript\">\n//<![CDATA[\nalert('All is good')\n//]]>\n</script>", "<script defer=\"defer\" type=\"text/javascript\">\n//<![CDATA[\nalert('All is good')\n//]]>\n</script>", "<script defer=\"defer\" type=\"text/javascript\">\n//<![CDATA[\nalert('All is good')\n//]]>\n</script>"]