我正在尝试按照此 Gist 显示 crud rails 应用程序https://gist.github.com/suryart/7418454的引导警报。其中的内容有代码:
application_helper.rb:
模块 ApplicationHelper
def bootstrap_class_for flash_type
{ success: "alert-success", error: "alert-danger", alert: "alert-warning", notice: "alert-info" }[flash_type] || flash_type.to_s
end
def flash_messages(opts = {})
flash.each do |msg_type, message|
concat(content_tag(:div, message, class: "alert #{bootstrap_class_for(msg_type)} fade in") do
concat content_tag(:button, 'x', class: "close", data: { dismiss: 'alert' })
concat message
end)
end
nil
end
应用程序.html.erb
<body>
<div class="container">
<%= flash_messages %>
<%= yield %>
</div><!-- /container -->
</body>
问题是我没有看到任何显示的消息。
我想可能是因为要点显示返回 nil,所以我可能需要返回 .each 迭代器的内容,所以我在帮助程序中做了类似的事情来返回 html:
def flash_messages
@flash_msg = ""
flash.each do |msg_type, message|
@flash_msg = concat(content_tag(:div, message, class: "alert #{bootstrap_class_for(msg_type)} fade in") do
concat content_tag(:button, 'x', class: "close", data: { dismiss: 'alert' })
concat message
end)
puts "@flash_msg: #{@flash_msg}"
end
@flash_msg
end
但是,当您查看 print 语句的输出时,它会显示页面的输入 HTML,其末尾实际上是我需要的 html:
<div class="alert alert-info fade in"><button class="close" data-dismiss="alert">x</button>Signed in successfully.</div>
如何让它工作,以便它只返回最后一部分而不是整个页面?