Forum: Ruby on Rails Custom flash[:notice]

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dylan M. (Guest)
on 2006-01-09 04:49
(Received via mailing list)
_______________________________________________
Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails
Cuong T. (Guest)
on 2006-01-09 05:13
(Received via mailing list)
How about using flash[:error]?
Francois B. (Guest)
on 2006-01-09 05:20
(Received via mailing list)
Hello Dylan,

2006/1/8, Dylan M. <removed_email_address@domain.invalid>:
>  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] = "<p class='red'>Error</p>" ? 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_plug...
Scott R. (Guest)
on 2006-01-09 06:02
(Received via mailing list)
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] = "<p
> class='red'>Error</p>" ? 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:

   <div class="flash warning">Warning here</div>
   <div class="flash notice">Notice here</div>

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.
This topic is locked and can not be replied to.