Rails闪光消息仍然是两页加载

我在Rails应用程序中使用Flash通知,使用以下代码:

flash[:notice] = "Sorry, we weren't able to log you in with those details." render :action => :new 

Flash消息按照“新build”动作呈现,然后在下一页显示用户访问(无论可能)。 它应该只显示一次,但有东西使它坚持。

有两种方法可以解决这个问题。 一个是使用

 flash.now[:notice] 

当你的闪存必须在当前请求结束时被丢弃,并且不打算在redirect之后使用。

第二个是打电话

 flash.discard(:notice) 

在请求结束时。 哪一个最好取决于你的应用程序行为。

http://api.rubyonrails.org/classes/ActionDispatch/Flash/FlashHash.html

好的,我解决了这个问题。 解决这个问题的方法是使用:

 flash.now[:notice] = "Sorry, we weren't able to log you in with those details." render :action => :new 

关键部分是flash.now [:notice]而不是flash [:notice]。