LoginSignup
12
14

More than 5 years have passed since last update.

Rails の flash メッセージを bootstrap-sass でスタイリング

Last updated at Posted at 2016-01-18

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 %>

になっていたため、空のハッシュ({})が画面に表示されて、「何だこれ??」としばらく悩んだ。

12
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
14