【问题标题】:Rails ApplicationHelper Escaping HTML without return statementRails ApplicationHelper 在没有返回语句的情况下转义 HTML
【发布时间】:2011-02-11 21:46:51
【问题描述】:
当我写作时
module ApplicationHelper
def flash_helper
flash.each do |key, msg|
content_tag :div, msg, :class => key
## "<div class=\"key\">" + msg + "</div>"
end
end
end
除非我return 声明,否则我什么也得不到。当我调用<%= flash_helper %> 时,HTML 在我看来是转义的。是什么赋予了?如何防止 HTML 被转义?
【问题讨论】:
标签:
html
ruby-on-rails
helpers
actionview
escaping
【解决方案1】:
你可以这样重写吗?
module ApplicationHelper
def flash_helper
s = ""
flash.each do |key, msg|
s += content_tag :div, msg, :class => key
## "<div class=\"key\">" + msg + "</div>"
end
return s
end
end
【解决方案2】:
您可以使用concat 方法(Rails >= 2.2.1)
module ApplicationHelper
def flash_helper
flash.each do |key, msg|
concat(content_tag :div, msg, :class => key)
end
nil
end
end