Created
July 13, 2011 15:58
-
-
Save rafaelp/1080597 to your computer and use it in GitHub Desktop.
Exemplo de uso de flash message em app Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# application.html.erb | |
<%= render_flash_messages %> | |
# application_helper.rb | |
def has_flash_messages? | |
flashes = [:alert, :notice, :warning, :message, :failure] | |
flash and flashes.find { |key| flash.has_key?(key) } | |
end | |
def render_flash_messages | |
return unless has_flash_messages? | |
buffer = "<div id=\"flash\">" | |
#[:alert, :notice, :warning, :message, :failure].each do |name| | |
flash.each do |name, text| | |
html_class = (:notice == name or :message == name) ? "flash_notice" : "flash_alert" | |
buffer << "<div id=\"messages\"><dl class=\"#{html_class}\"><dt>#{flash[name]}</dt></dl></div>" | |
end | |
buffer << "</div>" | |
buffer.html_safe | |
end | |
# application.css | |
#flash { | |
margin: 1em 0; | |
} | |
.flash_notice, | |
.flash_alert { | |
background-repeat: no-repeat; | |
background-position: 2em 50%; | |
margin: 1em 0; | |
padding: 1em 0 1em 5em; | |
border: solid 1px #338000 | |
} | |
.flash_alert { | |
color: red; | |
background-color: #ffc; | |
background-image: url(/images/flash_alert.gif); | |
border: solid 1px #f00 | |
} | |
.flash_notice { | |
color: red; | |
background-color: #e6ffd5; | |
background-image: url(/images/flash_notice.png); | |
border: solid 1px #338000 | |
} | |
.flash_notice dt, | |
.flash_alert dt { | |
color: black; | |
font-weight: bold; | |
margin: 0; | |
padding: 0; | |
width: 95% | |
} | |
.flash_notice dd, | |
.flash_alert dd { | |
width: 95% | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment