Rails 4.2.5 に bootstrap-sass 3.3.6 を導入してフラッシュメッセージをスタイリング。
こちらのページを参考にした(というか、ほぼそのまま)だけど、あとのコメントまで追って修正しないと正しく動作しなかったので、結論をメモ。
# helpers/application_helper.rb
def bootstrap_class_for flash_type
{ success: "alert-success", error: "alert-danger",
alert: "alert-warning", notice: "alert-info" }[flash_type.to_sym] || flash_type.to_s
end
def flash_messages(opts = {})
flash.each do |flash_type, message|
concat(
content_tag(:div, message, class: "alert alert-dismissable #{bootstrap_class_for(flash_type)} fade in") do
concat(
content_tag(:button, class: "close", data: { dismiss: "alert" }) do
concat content_tag(:span, "×".html_safe)
end
)
concat message
end
)
end
end
# views/layout/application.html.erb
<body>
...
<div class="container">
<% flash_messages %>
<%= yield %>
</div>
</body>
レイアウトの
<% flash_messages %>
が、元は
<%= flash_messages %>
になっていたため、空のハッシュ({})が画面に表示されて、「何だこれ??」としばらく悩んだ。