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 Notice/Warning Flash Message

  • submit to reddit
        Somewhat lame, but handy nonetheless.

<% if flash[:warning] or flash[:notice] %>
  <div id="notice" <% if flash[:warning] %>class="warning"<% end %>>
    <%= flash[:warning] || flash[:notice] %>
  <script type="text/javascript">
    setTimeout("new Effect.Fade('notice');", 15000)
<% end %>


Snippets Manager replied on Sun, 2010/11/07 - 3:56pm

Um using jQuery ($j) and Im putting everything inside the helper, like following: 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);") end

Snippets Manager replied on Fri, 2008/04/11 - 9:24am

Well in this particular case, where every other line already consists of code, it might be nicer to read it using the content_tag method. Most cases I'm al for using the normal html tag.

Snippets Manager replied on Mon, 2012/05/07 - 1:56pm

Sorry guys, but I have to put on a look of shock as I think anyone seriously uses: <%= content_tag :script, :type => "text/javascript" do -%> (!!)

Snippets Manager replied on Fri, 2008/04/11 - 9:24am

how about this? <% 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 - 9:24am

I tried this, I already did the looping over flash and using content tag, but the fade out I did in application.js with some ugly js code. Of course you need to remove the = sign from the second content tag, since it's a block. Otherwise you will get errors ;) Nice one though! Love it and using it right now... Only thing left to do it making it fade in before fading out :)

Snippets Manager replied on Mon, 2012/05/07 - 1:56pm

That's wonderful! Why didn't I think of that? :-)

Michael Christe... replied on Thu, 2007/11/29 - 11:45pm

There's a much easier way to do this, and you'll probably smack your head just seeing it. The first time I saw this work it was like tiny little sugary syntax explosions going off in my head. Here it is: <% 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. ;)