2

I'm trying to make rails' flash messages style with bootstrap 3.

In this piece of code,

    <% flash.each do |key, value| %>
      <div class="alert alert-<%= key %>"><%= value %></div>
    <% end %>

<%=key> puts the key into the class tag like you'd expect. But when I change the middle line to this

<div class="<%= flash_class(key) %>"><%= value %></div>

<%= flash_class(key)%> doesn't embed anything.

flash_class() is in application_helper.rb which is automatically included in views (right?) and it returns a string. I think it's probably something stupid I'm missing, why does this not work?

edit- here's the implementation of flash_class

   def flash_class(level)
        case level
            when :notice then "alert alert-info"
            when :success then "alert alert-success"
            when :error then "alert alert-error"
            when :alert then "alert alert-error"
        end
    end
5
  • 1
    what is your implementation of the flash_class method? Commented Jun 5, 2014 at 15:00
  • Can you add else "alert not recognized" and case level.to_sym? The else will return a default string if none is found, and the .to_sym will make sure the key is a symbol Commented Jun 5, 2014 at 15:10
  • Sounds a lot like a naming mismatch. Try adding a default case to flash_class just saying "#{level} not found". Commented Jun 5, 2014 at 15:11
  • 3
    First thing i always do in this sort of situation is to log the flash_class method, what is passed through for "level". You might find it's a string rather than a symbol. Commented Jun 5, 2014 at 15:31
  • That was exactly what happened. Commented Jun 5, 2014 at 15:38

1 Answer 1

2

Probably a problem comparing string with symbols:

def flash_class(level)
  case level.to_sym

Should solve your problem

Sign up to request clarification or add additional context in comments.

8 Comments

This is basically what I'm doing, only I'm formatting the class name to work with bootstrap. The bootstrap alert names and rails' flash keys aren't the same.
@NathanTempelman I updated my answer with an else statement in the case + .to_sym on level variable
I tried your else statement, and it put out "key not recognized". then I replaced it with level, and it came back as "notice". Then I tried your to_sym, and now it works. I guess you can't compare strings and symbols? And also I guess keys are always symbols?
Update your answer to include the symbol comparing shenannigan, and I'll accept it. Thanks for the help
No, keys can either be string or symbol. A HashWithIndifferentAccess is the same as a Hash except that you can access a value with either the key as a string or as a symbol. Your flash variable is a simple Hash, not HashWithIndifferentAccess -- @NathanTempelman my answer already uses a .to_sym on the key, what do you want me to change?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.