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 %>
Written by Milan Roy
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Flash
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#