如何在 Rails 4 中将 content_tag 与嵌套块一起使用?

How do I use content_tag with a nested block in Rails 4?

由于某种原因,这不起作用。如何像文档中显示的那样使用带有 content_tag 的块?

<% 
flash.each do |name, msg|
  if msg.is_a? Array
    concat content_tag(:div, :id => "flash_#{name}", class: 'alert bg-'+{'notice'=>'success', 'alert'=>'danger', 'error'=>'danger'}[name].to_s) do
      msg.each do |m|
        concat m
        concat "<br>".html_safe
      end
    end
  else
    concat content_tag :div, msg, :id => "flash_#{name}", class: 'alert bg-'+{'notice'=>'success', 'alert'=>'danger', 'error'=>'danger'}[name].to_s
  end
end 
%>

Documentation for content_tag

<%= content_tag :div, class: "strong" do -%>
  Hello world!
<% end -%>
 # => <div class="strong">Hello world!</div>

我的输出:

<div>{:id=&gt;"flash_alert", :class=&gt;"alert bg-danger"}</div>

控制器中设置闪光灯的行:

  flash.alert = @tracking.errors.full_messages

我在这里找到了答案:Rails' concat method and blocks with do...end doesn't work

块正在传递给 concat,而不是 content_tag。这有效:

concat content_tag(:div, :id => "flash_#{name}", class: 'alert bg-'+{'notice'=>'success', 'alert'=>'danger', 'error'=>'danger'}[name].to_s) {
  msg.each do |m|
    concat m
    concat "<br>".html_safe
  end
}