Last Updated: February 25, 2016
·
667
· cnascimentobr

Creating a helper for error notification in RoR

This is a simple article how to create a helper for error notification in RoR. I’m using Twitter bootstrap for styling the errors.

First open your application_helper.rb (helper folder) and create a function.

def flash_message
    messages = ""
    [:notice, :info, :warning, :error, :success].each {|type|
        if flash[type]
            messages += flash[type]
        end
    }
    messages 
end

Now you are able to call your recent flashmessage helper with each type (:notice, :info, :warning, :error, :success) you want. Ex: homecontroller.rb

def index 
    flash[:warning]="Your warning message" 
end

In your view:

<% flash.each do |type, message| %>
    <div class="alert alert-<%= type %> fade in">
        <button class="close" data-dismiss="alert">×</button>
        <%= message %>
    </div>
<% end %>

Done! ;)