Helper to Display Rails Flash Messages
A simple code snippet for displaying your
flash[:warning] = "Warning Message" messages in rails.
Add the following to your application_helper.rb.
To show all messages place this code in your view (rhtml).
Also here's some CSS to style the messages.
Posted on Wednesday, Dec 13, 2006

11 Comments
A few things should be noted:
flash_helper and render_flash_messages are roughly equivalent performance wiseflash_helper has a defect in it. Line 8 should read:
fl = fl + â€#{flash[name]}â€I should also note that content_tag is quite slow, which is why I used the inline string to generate HTML. If a partial was used instead (for the MVC purist, which I am usually) that would have incurred an even higher performance hit, which in most of my applications isn't worth the extra purity of the code.
def flash_helper
[:notice, :warning, :message].map { |f| content_tag(:div, flash[f], :class => type) if flash[f] }
end
def flash_helper
[:notice, :warning, :message].map { |f| content_tag(:div, flash[f], :class => f) if flash[f] }
end
I disagree... I think having a little markup in your helpers is perfectly acceptable. They are "help" for your presentation after all. I think the main mistake people make is using helpers for things that should go into a partial.
Leave a Comment