Over a million developers have joined DZone.

How To Handle Multiple Flash Keys

DZone's Guide to

How To Handle Multiple Flash Keys

Free Resource
posted by Scott Raymond on rails list

I generally like to add something like this to my application_helper.rb:

def flash_div *keys
  keys.collect { |key| content_tag(:div, flash[key],
                                   :class => "flash #{key}") if flash[key] }.join

...and then this in my layouts/application.rhtml:

<%= flash_div :warning, :notice %>

Now, if my controller puts anything into flash[:warning] or flash[:notice],
they'll render like:

Warning here
Notice here
Nice and DRY, and easy to style. If I ever need some other flash key besides :warning or :notice, I can just add an argument to the flash_div call and I'm set.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}