Last Updated: February 25, 2016
·
1.677K
· titas9x

Custom flash helper in rails4

adding custom flash attribute in rails 4 for bootstrap and foundation

class ApplicationController < ActionController::Base
  # ...
  add_flash_types :error, :success
end

if using bootstrap then use this

add_flash_types :error, :success, :info  

if using foundation then use this

add_flash_types :success :secondary 

using it with controller

class UsersController < ApplicationController
  #...
  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to @user, success: "User created!"
    else
      render :new
    end
  end
end

You will need a flash helper like this

#/app/view/layouts/application.html.erb
<% flash.each do |key, value| %>
  <div class="flash <%= key %>">
    <%= value %>
  </div>
<% end %>

for bootstrap use this

<% flash.each do |key, value| %>
  <div class="flash <%= "alert alert-#{key}" %>">
    <%= value %>
  </div>
<% end %>