Uber clean flash div in application helper

Tagged with Rails Ruby helper

Language: Ruby

View as text

# flash_div
# use to display specified flash messages
# defaults to standard set: [:success, :message, :warning]
# example:
#   <%= flash_div %>
# example with other keys:
#   <%= flash_div :notice, :violation %>
# renders like:
#   <div class="flash flash-success">Positive - successful action</div>
#   <div class="flash flash-message">Neutral - reminders, status</div>
#   <div class="flash flash-warning">Negative - error, unsuccessful action</div>

def flash_div(*keys)
  keys = [:success, :message, :warning] if keys.empty?
  keys.compact.collect do |key|
    flash[key].blank? ? nil : content_tag(:div, flash[key], :class => "flash flash-#{key}")
  end.compact.join("\n")
end
Original snippet written by Jon Kinney
Last updated at 10:20 AM on Aug 08, 2008

SnippetStash costs money to host and develop. The service is free for everyone to use
but if you found it useful please consider making a small donation.