Rails flash 消息保留两个页面加载 [英] Rails flash message remains for two page loads

查看:33
本文介绍了Rails flash 消息保留两个页面加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using a flash notice in a Rails application, with the following code:

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

Flash 消息在新"操作上按预期呈现,但随后它还会在用户访问的下一页上显示(无论是什么).它应该只显示一次,但有些东西让它一直存在.

The flash message renders as expected on the 'new' action, but then it also shows on the next page the user visits (whatever that might be). It should only show once, but something's making it stick around.

推荐答案

解决这个问题有两种方法:

There are two ways to solve this problem:

一个是使用

flash.now[:notice]

当您的 flash 必须在当前请求结束时被丢弃并且不打算在重定向后使用时.

when your flash must be discarded at the end of the current request and is not intended to be used after a redirect.

第二个是打电话

flash.discard(:notice)

在请求结束时.

标准 Flash 消息旨在为下一个"请求保留.例如.您在处理创建或编辑请求时生成 Flash,然后将用户重定向到显示屏幕.当浏览器向显示屏幕发出下一个请求时,就会显示 flash.

The standard flash message is intended to be kept for the "next" request. E.g. you generate a flash while handling a create or edit request, then redirect the user to the show screen. When the browser makes the next request to the show screen, the flash is displayed.

如果您确实在显示屏幕上生成了 Flash,请使用 flash.now.

If you actually generate a flash on the show screen itself, use flash.now.

查看 Ruby on Rails API 文档以了解 Flash 哈希的工作原理

这篇关于Rails flash 消息保留两个页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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