Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flash message does not have bootstrap classes

I have an view where I want to display flash message for some events. I can see the flash message but not in color as it should with bootstrap. for example green for success, blue for notice etc. but I see everything in white.

Here is my code:

<% flash.each do |name, msg| %>
   <%= content_tag :div, msg, class: name %>
<% end %>

PS: it is not full code, just code snippet to give idea, other bootstrap classes are working but here except message in ugly white background I do not get anything. Any suggestions?

like image 660
user3290805 Avatar asked Aug 31 '26 17:08

user3290805


1 Answers

You can use a helper method for assigning proper bootstrap classes according to the type of flash notification messages:

def alert_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 then use it as:

<% flash.each do |name, msg| %>
   <%= content_tag :div, msg, class: [:alert, alert_for(name)]%>
<% end %>
like image 78
potashin Avatar answered Sep 03 '26 09:09

potashin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!