Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Refactoring the Flash Code

Hey folks,

Following the "Refactoring the Flash Code" video I was unable to get the alert classes to show correctly, Its one of those frustrating ones that doesn't give you an error to fix it just isn't showing the colors assigned to classes (green for -success etc).

I have been through this video several times and have the exact code that Jim uses, It may be one of the little rails 4 issues but im wondering if anyone has a solution for this?

The correct :alerts & :notice text are displaying, just with no bootstrap classes to color them??

Here is the code I have...

module ApplicationHelper

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

end

cheers!

application.html.erb

<div class="container">

<% flash.each do |type, message| %>
    <div class="alert <%= flash_class type %> alert-dismissable">
  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
  <%= message %>
</div>
<% end %>

</div>

6 Answers

You could also do it this way in Rails 4 to avoid the "then"s I originally used:

module ApplicationHelper

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

For Rails 4 I did it this way and it worked:

module ApplicationHelper

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

Hope this helps.

Sean Perryman
Sean Perryman
13,810 Points

Is there any particular way the passing of the object doesn't work in Rails 4 like it seems to in Rails 3?

Awesome, thanks for the post Todd Nestor. Totally worked.

No problem Anthony Gonzales , good luck with the rest of the tutorial.

Thanks Todd Nestor! So simple, but i never would have got it without your help.