DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Helper To Display Rails Flash Messages

12.13.2006
| 6205 views |
  • submit to reddit
        A simple code snippet for displaying your flash[:warning] = "Warning Message" messages in rails.

def flash_helper
  
    f_names = [:notice, :warning, :message]
    fl = ''
    
    for name in f_names
      if flash[name]
        fl = fl + "<div class=\"notice\" id=\"#{name}\">#{flash[name]}</div>"
      end
      flash[name] = nil;
    end
    return fl
  end
    

Comments

Snippets Manager replied on Thu, 2006/12/14 - 3:52pm

A slightly more terse way of doing this: def show_flash [:notice, :warning, :message].collect do |key| content_tag(:div, flash[key], :class => "flash flash_#{key}") unless flash[key].blank? end.join end

Snippets Manager replied on Fri, 2006/02/17 - 10:49am

Full snippet available here