技術ブログっぽいなにか

Ruby・Railsメインでいろんなこと書きます

deviseのflashをBootstrap対応にする

deviseとBootstrapを組み合わせて使っていると、deviseとBootstrapで表現が違うため色がいい感じにならない時がある
での、それを解決します

application.html.erb

<% flash.each do |name, msg| %>
    <div id="alert" class="col-xs-11 alert alert-<%= bootstrap_class_for(name) %> alert-dismissible fade" >
      <a class="close" data-dismiss="alert" aria-label="Close">×</a>
      <%= msg %>
    </div>
<% end %>

application_helper.rb

module ApplicationHelper
  def bootstrap_class_for(flash_type)
      case flash_type
          when "success"
          "success"
          when "error"
          "danger"
          when "alert"
          "warning"
          when "notice"
          "info"
          else
          flash_type.to_s
      end
  end
end

こうしておくと、これから色の種類が増えても修正が楽になるね!

参考

hitori-programming.org