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
Rails Notice/Warning Flash Message
Somewhat lame, but handy nonetheless.
<% if flash[:warning] or flash[:notice] %>
<div id="notice" <% if flash[:warning] %>class="warning"<% end %>>
<%= flash[:warning] || flash[:notice] %>
</div>
<script type="text/javascript">
setTimeout("new Effect.Fade('notice');", 15000)
</script>
<% end %>






Comments
Snippets Manager replied on Sun, 2010/11/07 - 4:56pm
def flash_message messages = "" [:notice, :info, :warning, :error].each {|type| if flash[type] messages += content_tag :div, flash[type], :class => "#{type}", :id => "alert" end } messages << javascript_tag("$j('#alert').fadeOut(12000);") endSnippets Manager replied on Fri, 2008/04/11 - 10:24am
Snippets Manager replied on Mon, 2012/05/07 - 2:56pm
<%= content_tag :script, :type => "text/javascript" do -%>(!!)Snippets Manager replied on Fri, 2008/04/11 - 10:24am
<% flash.each do |key, msg| %> <%= content_tag :div, msg, :class => "flash", :id => key %> <% content_tag :script, :type => "text/javascript" do %> $('<%= key %>').style.display = 'none'; new Effect.Appear('<%= key %>', {duration: 3}); <% end %> <% content_tag :script, :type => "text/javascript" do %> setTimeout("new Effect.Fade('<%= key %>');", 10000); <% end %> <% end %>for some reason it did not work when I just set the .flash class in my stylesheet to display: none;Snippets Manager replied on Fri, 2008/04/11 - 10:24am
Snippets Manager replied on Mon, 2012/05/07 - 2:56pm
Michael Christe... replied on Fri, 2007/11/30 - 12:45am
<% flash.each do |key, msg| %> <%= content_tag :div, msg, :class => [key, " message"], :id => "notice_ #{key}" %> <%= content_tag :script, :type => "text/javascript" do -%> setTimeout("new Effect.Fade('notice_<%= key %>');", 15000); <% end %> <% end %>Now you have a completely flexible solution. Add as many key to your flash as you want and you only need those 6 lines of code to handle it all; be it errors, notices, warnings, or some uber prize alert! ** WARNING ** : Use with caution. Sugary syntax can be highly addictive. Use with discretion. ;)