user3290805
user3290805

Reputation: 475

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?

Upvotes: 3

Views: 1035

Answers (2)

potashin
potashin

Reputation: 44581

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 %>

Upvotes: 7

rxing
rxing

Reputation: 373

I guess you should give the correct css class.

"name" here should be replaced by like 'alert alert-success'.

Upvotes: 0

Related Questions