David Dsr
David Dsr

Reputation: 347

Apply Bootstrap style to flash error

I'm using Bootstrap to my page and I'm having troubles with the flash error. I need all the flashes get Bootstrap style, so I've made a helper module to do it. The Flash notice works ok with the style, but when I force the error, it just come up "level can't be blank" without style. Tnis is my code:

_flash_messages.html.erb

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

application_helper.rb

module ApplicationHelper

def bootstrap_class_for flash_type
if flash_type == "success"
    "alert-success"
elsif flash_type == "error"
    "alert-error"
elsif flash_type == "alert"
    "alert-block"
elsif flash_type == "notice"
    "alert-info"
else
    flash_type.to_s
end
end

end

_form.html.erb

<% if @control.errors.any? %>
<div class="alert alert-error fade in">

<% @control.errors.full_messages.each do |error_message| %>
   <p><%= error_message %></p> 
</div>
<% end %>

<% end %>

Upvotes: 1

Views: 741

Answers (1)

Benjamin J. B.
Benjamin J. B.

Reputation: 1181

I use this helper:

  def alert_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

And in my view (I use Slim, let me know if you want me to unslim it):

 - flash.each do |type, message|
   .alert.alert-dismissible.fade.in class=(alert_class_for(type))
     = message

Upvotes: 2

Related Questions