Ian Purton

Ian Purton’s Notes

Helper to Display Rails Flash Messages

A simple code snippet for displaying your flash[:warning] = “Warning Message” messages in rails.

Add the following to your application_helper.rb.

def flash_helper

    f_names = [:notice, :warning, :message]
    fl = ''

    for name in f_names
      if flash[name]
        fl = fl + "<div class=\"notice\">#{flash[name]}</div>"
      end
    flash[name] = nil;
  end
  return fl
end

To show all messages place this code in your view (rhtml).

<%= flash_helper %>

Also here’s some CSS to style the messages.

div.notice {
	margin-left: auto;
	margin-right: auto;
	text-align: center;
	width: 40%;
	border: 5px solid #ccc;
	margin-top: 50px;
	padding: 20px;
	font-weight: bold;
}

"Helper to Display Rails Flash Messages" was published on December 13th, 2006 and is listed in Webmastering, rails.

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Helper to Display Rails Flash Messages: 8 Comments

  1. Thank You

  2. Dallas

    You really should not put xhtml code into your helpers. Keep your content, layout, functional code, and styles separate.

  3. I use a more terse version of what your code does:

      def render_flash_messages
        result = ''
        for type in [:notice, :warning, :message]
          message = flash[type]
          # commented out content_tag, while cleaner is very inefficient
          #content_tag('div', message, :class => type) if message
          # using string inline method...
          result #{message}"
        end
        result
      end
    

    A few things should be noted:
    flash_helper and render_flash_messages are roughly equivalent performance wiseflash_helper has a defect in it. Line 8 should read: fl = fl + ”#{flash[name]}”

  4. Formatting of previous comment (or lackthereof) may cause misunderstanding. Code should read:

    def render_flash_messages
      result = ''
      for type in [:notice, :warning, :message]
        message = flash[type]
        result #{message}”
      end
      result
    end
    

    I should also note that content_tag is quite slow, which is why I used the inline string to generate HTML. If a partial was used instead (for the MVC purist, which I am usually) that would have incurred an even higher performance hit, which in most of my applications isn’t worth the extra purity of the code.

  5. here’s another version of the above, even more terse…

    def flash_helper
    [:notice, :warning, :message].map { |f| content_tag(:div, flash[f], :class => type) if flash[f] }
    end

  6. woops, I mean

    def flash_helper
    [:notice, :warning, :message].map { |f| content_tag(:div, flash[f], :class => f) if flash[f] }
    end

  7. @Dallas

    I disagree… I think having a little markup in your helpers is perfectly acceptable. They are “help” for your presentation after all. I think the main mistake people make is using helpers for things that should go into a partial.

  8. OK, let me repeat that… Do you have a sense of wonder about my comfortable doubt A JOKE! ) What is three feet long? A yard.

Leave Your Comment

(required)
(required)
 

Subtraction Wordpress Theme by Ian Purton