从ApplicationController重定向时,如何防止flash [:notice]呈现两次

时间:2010-01-07 17:36:04

标签: ruby-on-rails ruby

我在ApplicationController中有一个简单的方法,在调用时,可以设置'flash [:notice]'然后重定向到root_url。

问题在于,即使该方法只调用一次,根URL也会呈现闪存[:notice] TWICE。

这是一个方法(在其他控制器中使用的before_filter,在ApplicationController中定义):

def authenticate
  if params[:id].try(:size) == 40
    company = Company.find_by_hash_identifier(params[:id])
    if company
      session[:editable_companies] ||= []
      session[:editable_companies] << company.id
      session[:editable_companies].compact!.uniq!
    end
  end
  unless session[:editable_companies].try('&', [company.try(:id), params[:id]])
    flash[:notice]= "You are not permitted to edit this company.<br />Please check the URL from the email we sent you, and try again."
    flash.keep[:notice]
    redirect_to root_url and return
  end
end

在root_url视图中,我得到两次闪现:

您无权修改此公司。您无权修改此公司。

3 个答案:

答案 0 :(得分:1)

摆脱flash.keep(:notice)行。

答案 1 :(得分:1)

您(至少不应该)需要调用flash.keep(:notice)来将Flash存储在重定向中。闪存哈希值中的值仅在render上自动删除。

答案 2 :(得分:-4)

原来这是视图中的一个问题。 : - (

我实际上曾两次闪过[:notice]。