Rails: Easy way to add more than one flash[:notice] at a time

I usually add such methods to my ApplicationHelper:

def flash_message(type, text)
    flash[type] ||= []
    flash[type] << text
end

And

def render_flash
  rendered = []
  flash.each do |type, messages|
    messages.each do |m|
      rendered << render(:partial => 'partials/flash', :locals => {:type => type, :message => m}) unless m.blank?
    end
  end
  rendered.join('<br/>')
end

And after it is very easy to use them:

You can write something like:

flash_message :notice, 'text1'
flash_message :notice, 'text2'
flash_message :error, 'text3'

in your controller.

And just add this line to your layout:

<%= render_flash %>

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)