Rails Flash Mesages with Bootstrap Alerts


Quick copypasta to display flash messages in Bootstrap 3 alert styles

In ApplicationHelper

module ApplicationHelper
  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
end

Create a partial app/views/shared/_flash.html.slim

- flash.each do |flash_type, message|
  .alert.alert-dismissable class="#{bootstrap_class_for(flash_type)}"
    button.close data-dismiss='alert' ×
    = message

And just render it whenever needed

= render 'shared/flash'

The idea is taken from https://gist.github.com/suryart/7418454