Never been to DZone Snippets before?

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 (See related posts)

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 on this post

m3talsmith posts on Mar 23, 2008 at 20:44
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. ;)
timmorgan posts on Mar 24, 2008 at 09:18
That's wonderful! Why didn't I think of that? :-)
nyxx posts on Apr 11, 2008 at 10:26
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 :)
nyxx posts on Apr 11, 2008 at 10:35
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;
peter posts on Apr 11, 2008 at 18:17
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 -%>


(!!)
nyxx posts on Apr 13, 2008 at 12:24
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 <script> html tag.

You need to create an account or log in to post comments to this site.


Click here to browse all 4841 code snippets

Related Posts