为什么闪信不会消失? [英] Why flash message won't disappear?

查看:48
本文介绍了为什么闪信不会消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的控制器中做一些异常处理,当在 :create 动作中抛出异常时,我将渲染到 :new 动作并显示一条 flash 消息.

I am doing some exception handling in my controller, when there is an exception thrown in :create action, I will render to the :new action and show a flash message.

一切正常,捕获异常时我可以看到闪现消息,但是当我重定向到(手动点击)其他页面时,闪现消息仍然在这里.然后我重定向到另一个页面(第二次点击),消息可能会消失.

Everything works fine, I can see the flash message when exception caught, but when I redirect to(handly click) other page, the flash message still here. Then I redirect to another page(the second handly click), the message could disappear.

谁知道是什么原因?

我的控制器代码:

class MessagesController < ApplicationController
  rescue_from Exception, :with => :render_new

  def new
  end

  def create
  end

private
  def render_new
    flash[:alert] = t("uploading_error")
    render :action => :new
  end
end

我的布局代码(Haml):

My layout code (Haml):

%body
  #content
    - unless flash[:alert].blank?
      #alert= flash[:alert]

推荐答案

替换

flash[:alert] = t("uploading_error")

flash.now.alert = t("uploading_error")

看看这是否是您期望的结果?

and see if that is the result you expect?

flash[:alert] 将留在下一页(因此它只会在第二次重定向时消失);但 flash.now.alert 只会显示当前页面.

flash[:alert] will stay around for the next page (hence it only disappears at the second redirect); but flash.now.alert will only display for the current page.

这篇关于为什么闪信不会消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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