Rails:一次添加多个 flash[:notice] 的简单方法
我想每次你执行 flash[:notice]="Message"
时,它都会将它添加到数组中,然后在视图中显示,但以下内容只保留最后一次闪烁:
I thought every time you do a flash[:notice]="Message"
it would add it to the array which would then get displayed during the view but the following just keeps the last flash:
flash[:notice] = "Message 1"
flash[:notice] = "Message 2"
现在我意识到这只是一个带键的简单散列(我认为:))但是有没有比以下更好的方法来进行多次闪烁:
Now I realize it's just a simple hash with a key (I think :)) but is there a better way to do multiple flashes than the following:
flash[:notice] = "Message 1<br />"
flash[:notice] << "Message 2"
flash
消息真的可以是你想要的任何东西,所以你可以这样做:
The flash
message can really be anything you want, so you could do something like this:
flash[:notice] = ["Message 1"]
flash[:notice] << "Message 2"
然后在您的视图中,将其输出为
And then in your views, output it as
<%= flash[:notice].join("<br>") %>
或者任何你喜欢的.
该技术是否比其他解决方案更容易取决于您自己的喜好.
Whether that technique is easier than other solutions is determined by your own preferences.