Rails:一次添加多个 flash[:notice] 的简单方法 [英] Rails: Easy way to add more than one flash[:notice] at a time

查看:32
本文介绍了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.

这篇关于Rails:一次添加多个 flash[:notice] 的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆