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

Rails Flash Messages Helper

11.07.2008
| 5403 views |
  • submit to reddit
        in your view:

display_flash(:error) # to display a specific flash message
display_flash # to display all flash messages

# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  
  FLASH_TYPES = [:error, :warning, :success, :message]

  def display_flash(type = nil)
    html = ""
    
    if type.nil?
      FLASH_TYPES.each { |name| html << display_flash(name) }
    else
      return flash[type].blank? ? "" : "<div class=\"#{type}\"><p>#{flash[type]}</p></div>"
    end
    
    html
  end
  
end