Custom flash[:notice]


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

How about using flash[:error]?

Hello Dylan,

2006/1/8, Dylan M. [email protected]:

For the most part, my flash[:notice] messages fall into one of two
categories: either a message stating that something happened successfully
(which is colored green), or messages showing errors (which I would like to
be red).

Is it possible to set some sort of flag on these messages to choose which
color I want to show, or would I need to base it on CSS and set the class in
the text, i.e. flash[:notice] = “

Error

” ? Thanks!

There’s nothing preventing you from adding more than one type of
message to the flash. For example, the Flash Helper Plugin[1] adds up
to three types of messages to the flash: notice, message and warning.
These are then marked up appropriately with CSS classes while being
displayed.

Hope that helps !

François Beausoleil
http://blog.teksol.info/

[1] https://opensvn.csie.org/traccgi/flash_helper_plugin/trac.cgi/

Dylan M. <dylan@…> writes:

Is it possible to set some sort of flag on these messages to choose
which color I want to show, or would I need to base it on CSS and set
the class in the text, i.e. flash[:notice] = “

Error

” ? Thanks!

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
end

…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.

Scott R.
scottraymond.net, redgreenblu.com, blinksale.com, iconbuffet.com.