Mario
Mario

Reputation: 1253

Flash messages does not match

I have this code:

    <% flash.each do |type, message| %>
        <div class="alert <%= flash_class type %>">
            <button class="close" data-dismiss="alert">x</button>
            <%= message + " " + type %>
        </div>
    <% end %>

to display flash messages, and in the application_helper I have this:

module ApplicationHelper    

def flash_class(type)
        case type
        when :alert 
            "alert-danger"
        when :notice 
            "alert-success"
        else
            "alert-info"
        end
    end    
end

If I intentionally do this <div class="alert <%= flash_class :alert %>"> it displays with the proper colour (result of the correct class in the html). Otherwise I always get this result:

enter image description here

I'm new at ruby-on-rails so I must have missed something simple, but I don't know what. Any help? I'm using ruby 2.1.2 and rails 4.1.5.

Upvotes: 0

Views: 37

Answers (1)

Nick Veys
Nick Veys

Reputation: 23939

The flash type has changed in (I think) Rails 4, it's a string, you're comparing symbols, so it'll never match.

Try this, it'll handle both:

def flash_class(type)
    case type
    when :alert, 'alert'
        "alert-danger"
    when :notice, 'notice'
        "alert-success"
    else
        "alert-info"
    end
end

Upvotes: 3

Related Questions