LoginSignup
22
23

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-08-29

僕は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
22
23
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
22
23