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 Build a Simple Ruby on Rails Application Designing URLs Refactoring the Flash Code

Gareth Kavanagh
Gareth Kavanagh
9,329 Points

erb problem with helper method

Hi, I'm trying to get the correct class returned so that the flash message is style appropriately, but I always get nothing returned from the helper method. I know that the type is getting to the helper method correctly but I am not getting the correct response from flash_class. This is the relevant part of my application.html.erb

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

this is my helper

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

can anyone see what's wrong with it? I am using rails 4.0.1, could that be the cause of it?

Is your helper method in the correct file?

Gareth Kavanagh
Gareth Kavanagh
9,329 Points

It's in the application_helper.rb file. I believe that's the correct file and I added a puts line into it to output the type, this is being output to the server terminal when the flash is displayed so it's being called correctly.

Could be a css issue? Does the flash message div display when you view the source of the page? If you're using bootstrap you may need to add a margin-top of about 50px to the alert class or flash class type

Gareth Kavanagh
Gareth Kavanagh
9,329 Points

I've figured it out, it's because of the way the case is being checked in the helper instead of it being when :alert it should be when "alert". I don't know why it works the other way in the video maybe it's down to the version of rails but I kind of doubt that.

Anyway, thanks for trying to help Twiz T. I appreciate it!

2 Answers

Gareth Kavanagh
Gareth Kavanagh
9,329 Points

I've figured it out, it's because of the way the case is being checked in the helper instead of it being when :alert it should be when "alert". I don't know why it works the other way in the video maybe it's down to the version of rails but I kind of doubt that.

Anyway, thanks for trying to help Twiz T. I appreciate it!

edit: move to comment