我在 Rails 应用程序中使用 flash 通知,代码如下:
flash[:notice] = "Sorry, we weren't able to log you in with those details."
render :action => :new
Flash 消息在"new"操作中按预期呈现,但随后它还会显示在用户访问的下一页(无论可能是什么)上。它应该只显示一次,但有些东西让它一直存在。
最佳答案
有两种方法可以解决这个问题:
一种是使用
flash.now[:notice]
当您的 flash 必须在当前请求结束时被丢弃并且不打算在重定向后使用时。
第二个是调用
flash.discard(:notice)
在请求的末尾。
标准的 flash 消息旨在为“下一个”请求保留。例如。您在处理创建或编辑请求时生成一个 flash,然后将用户重定向到显示屏幕。当浏览器向显示屏幕发出下一个请求时,将显示 flash。
如果您实际在显示屏幕上生成 flash,请使用 flash.now
。
Check the Ruby on Rails API documentation to see how the Flash hash works
关于ruby-on-rails - Rails flash 消息保留两个页面加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1017210/