Displaying Flash’s in Rails easily
Rails can handle multiple Flash states in the flash hash, e.g. :notice, :error, :success. But how do you display these messages in a no nonsense fashion?
In all my Rails applications I make use of the Flash method for displaying simple messages about system changes to the user, such as “Welcome back Ivan” when they login, or “Your details have been saved successfully” when the user saves their profile.
Rails can handle multiple Flash states in the flash hash, e.g. :notice, :error, :success. But how do you display these messages in a no nonsense fashion? with this simple display_flashes helper method I’ve written.
1 2 3 4 5 6 7 8 9 10 11 12 | module ApplicationHelper
def display_flashes
return if flash.blank?
flash_type = flash.keys.first.to_sym
return content_tag(:div, :id => "flash_box", :class => "flash-wrapper") do
content_tag(:div, flash[flash_type], :class => "flash #{flash_type}")
end
end
end |