Help us understand the problem. What is going on with this article?

RailsでTwitterBootstrapを使ったflashメッセージ

More than 5 years have passed since last update.

僕はbootstrapをよく使うので、bootstrapのAlerts Componentを使ってflash部分をpartial化してみる。
もちろんbootstrapじゃなくても、flashのタイプごとにhtmlとかcssを変えたい場合に応用できると思う。

_flashes.html.erb
<% flash.each do |type, message| %>
<div class="alert <%= flash_class_for(type) %>" role="alert">
  <button class="close" data-dismiss="alert">×</button>
  <%= message %>
</div>
<% end %>
application.html.erb
<%= render partial: "shared/flashes", flash: flash %>
application_helper.rb
module ApplicationHelper
  def flash_class_for flash_type
    case flash_type
    when 'success' then 'alert-success'
    when 'error'   then 'alert-danger'
    when 'alert'   then 'alert-warning'
    when 'notice'  then 'alert-info'
    end
  end
end
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away