【问题标题】:Flash notice with redirect_to is broken in rails带有redirect_to 的Flash 通知在rails 中被破坏
【发布时间】:2010-10-26 01:09:57
【问题描述】:

我更新到 Rails 2.3.10,Rack 1.2.1,现在我的 flash 消息都没有出现。我发现在重定向期间通知是这样传递的

redirect_to(@user, :notice => "Sorry there was an error")

在我看来,闪存哈希是空的

<%= debug flash %>  
!map:ActionController::Flash::FlashHash {} 

但你可以在控制器中看到消息。给出了什么?

        <%= debug controller.session %>
        session{:home_zip=>"94108", :session_id=>"xxx", :flash=>{:notice=>"Sorry there was an error"}, :user_credentials=>"1baf9c9c0423ce0151ec32e24cc422f07309e4ba503eb4830635ecc115da217809997324374bb273b3fb792895c9741a8b8c9ea4267771a1bd149de5b9179ea0", :user_credentials_id=>22, :home_market_id=>11}
        Edit Profile    

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    您检查过 Rails 错误跟踪器吗?我仍然使用老式的 setter flash[:notice] = message 并且它工作正常,所以它似乎是一个redirect_to 方法的问题。

    https://rails.lighthouseapp.com/

    您是否尝试过 redirect_to url, :flash =&gt; { :notice =&gt; "notice" } 作为解决方法?

    【讨论】:

    • 谢谢,但我也试过了,但看起来从视图调用的 flash 方法已损坏。
    【解决方案2】:

    下面的代码应该可以工作:

    redirect_to(@user, {:notice =&gt; "Sorry there was an error"})

    我猜这是由于 Ruby 而不是 Rails 的变化,因为它看起来像是编译器中的令牌解析优先级变化。

    【讨论】:

    • 它对我有用,在我的情况下,它是导致问题的闪光灯之前的model_path model
    【解决方案3】:

    我们也遇到了这个问题。我们所有的 flash 消息都会随着重定向消失,但在控制器中明确设置时不会。

    不起作用:

      def create
        if @obj.save
          flash[:notice] = "The #{cname.humanize.downcase} has been created."
          redirect_back_or_default redirect_url
        else
          render :action => 'new'
        end
      end
    

    这确实有效:

     def show
        @user = current_user
        flash[:notice] = "Hello -- this will show up fine"
      end
    

    【讨论】:

      【解决方案4】:

      这可能是 cookie 的问题。长话短说,如果您之后立即重定向,cookie 不会得到。假设 Rails 使用 cookie 实现 flash,那么重定向是你的问题。

      来源:

      http://persistall.com/archive/2008/01/25/cookies--redirects--nightmares.aspx http://stackoverflow.com/questions/1621499/why-cant-i-set-a-cookie-and-redirect

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多