我以为每次你做一次flash[:notice]="Message"
,它都会把它添加到数组中,然后在视图中显示出来,但下面的代码只是保留了最后一次闪光:
flash[:notice] = "Message 1"
flash[:notice] = "Message 2"
现在我意识到这只是一个简单的散列和一个键(我认为:)),但有比下面更好的方法来进行多次刷新:
flash[:notice] = "Message 1<br />"
flash[:notice] << "Message 2"
发布于 2010-03-15 16:43:04
flash
消息实际上可以是您想要的任何内容,所以您可以这样做:
flash[:notice] = ["Message 1"]
flash[:notice] << "Message 2"
然后在您的视图中,输出为
<%= flash[:notice].join("<br>") %>
或者你喜欢的任何东西。
该技术是否比其他解决方案更容易取决于您自己的喜好。
发布于 2010-03-16 08:22:29
我通常在我的ApplicationHelper中添加这样的方法:
def flash_message(type, text)
flash[type] ||= []
flash[type] << text
end
和
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
在使用起来非常简单之后:
您可以编写如下内容:
flash_message :notice, 'text1'
flash_message :notice, 'text2'
flash_message :error, 'text3'
在你的控制器里。
然后将这一行添加到您的布局中:
<%= render_flash %>
发布于 2011-08-03 19:11:35
我认为构建在框架中的想法是,你粘贴到flash中的每一条消息都是可重写的。您可以为每条消息提供一个唯一的键,以便您可以更改或覆盖它。
如果您需要另一条消息,请不要将其称为“:通知”。每一个都是独一无二的。然后,为了呈现flash消息,循环遍历散列中的任何内容。
如果这对你不起作用,考虑你是否真的需要简化你的UI。
https://stackoverflow.com/questions/2448789
复制相似问题