4

This is what I wrote in a controller file, but only 1 notice (the last one) would appear.

flash[:notice] ="a: " + aa.to_s
flash[:notice] ="b: " + bb.to_s
flash[:notice] ="c: " + cc.to_s

I want to show all of the three notice together (at one time), is there anyway to achieve this?

in my html:

<% if notice %>
  <p id="notice"><%= notice %></p>
<% end %>
Seth
  • 10,198
  • 10
  • 45
  • 68
user2049259
  • 533
  • 2
  • 6
  • 13
  • You can also use plain old String concatenation: http://stackoverflow.com/questions/377768/string-concatenation-and-ruby – jyrkim Apr 27 '14 at 09:33

3 Answers3

6

You could try like this

flash[:notice] = ["a: " + aa.to_s]
flash[:notice] << "b: " + bb.to_s
flash[:notice] << "c: " + cc.to_s

And then in your views, output it as

<%= flash[:notice].join("<br>") %>

or whatever you want in your view

LHH
  • 3,233
  • 1
  • 20
  • 27
  • @Niall and me has given answer at same time..i think only few seconds difference....:) – LHH Apr 27 '14 at 09:37
  • Thank you very much. It works, but it seems that the "
    " works not as a new line, but a string in html... But it is ok, I changed it to /// so the result looks nice!
    – user2049259 Apr 27 '14 at 09:55
  • 1
    you could use .html_safe to output it properly – Niall Paterson Apr 27 '14 at 09:58
  • Yes @Niall is right.....you could use html_safe and this way, you can return HTML Safe strings – LHH Apr 27 '14 at 10:00
  • Oh, there is one more question, I found that all of the other notice does not work (error occurs). For example, "xxx was successfully removed" does not work. error: undefined method `join' for "Your food list is currently empty":String – user2049259 Apr 27 '14 at 11:08
4

You can do this:

 flash[:notice] = ["a: " + aa.to_s]
 flash[:notice] << "b: " + bb.to_s
 flash[:notice] << "c: " + cc.to_s

And then:

 <% if notice %>
     <p id="notice"><%= notice.join("<br>").html_safe %></p>
 <% end %>

This is essentially because flash[:notice] can be any kind of object, so we can make it an array to work with multiple notices.

Niall Paterson
  • 3,580
  • 3
  • 29
  • 37
2

Another way is to write helper methods like this

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

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

And in your controller do like this

flash_message :notice, 'msg1'
flash_message :notice, 'msg2'
flash_message :notice, 'msg3'

And in your view,you can just add like this

<%= multiple_flash %>
Pavan
  • 33,316
  • 7
  • 50
  • 76
  • Thank you, where should I input the two methods? in model folder? or helper folder? I am new to rails, so excuse me.. – user2049259 Apr 27 '14 at 11:14
  • The methods goes to `helper` and `flash_message :notice, 'msg1'` stuff goes to your `controller` and the last line to your `view`. – Pavan Apr 27 '14 at 11:27
  • For all those who also want to use `flash.now`: You will need to add in the line `flash.now[type.to_sym] ||= []`. This must go before `flash[type] ||= []`. – br3nt Dec 22 '16 at 22:36