I am currently trying to display a message every time a custom method (process!) returns either false or true within my transaction controller. However, it is only returned once for every false and once for every true. Below is the code in the controller:
def execute_all
@transaction = Transaction.find(:all)
#Execute all transactions
@transaction.each do |t|
if (t.process!)
#flash.keep[:noticeTransaction] = 'Transaction number: ' + t.id.to_s + ' executed Successfully!'
else
flash.keep[:errorTransaction] = 'Transaction cannot be executed -> Transaction Id: ' + t.id.to_s
end
end
respond_to do |format|
format.html { redirect_to transactions_url }
format.json { head :no_content }
end
Below is the code in the application.html.erb
<html>
<head>
</head>
<body>
<p style="color:red" class="error"><%= flash[:errorTransaction] %></p>
<p style="color:green" ><%= flash[:noticeTransaction] %></p>
<%= yield %>
</body>
I assuming since I only mention it once in the application layout (one for error and one for success) it display it only once. I am wondering how would I have it to display for each false that is returned by the method “process!”.
Thanks in advance.
The layout displays only one, because there is only one.
flashstores one message per key, regardless of whether you usekeepor not.So each time you set
flash.keep[:errorTransaction], you’re overwriting the previous message, not appending another one.To solve this you could store all the messages as you iterate over the transactions, then store them on the
flashall at once, something like: