【问题标题】:Why flash message won't disappear?为什么闪信不会消失?
【发布时间】:2011-06-04 13:52:43
【问题描述】:

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

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

谁知道是什么原因?

我的控制器代码:

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):

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

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    替换

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

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

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

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

    【讨论】:

    • 优秀的解决方案和链接!谢谢zabba!
    • 现在我对 flash 和 flash.now 有了更多的了解。所以如果我使用redirect_to而不是render,使用flash也没有问题。
    • 仅供参考:链接已损坏。但是没有看到链接,我不得不承认我很好奇为什么 flash.now[:alert] 不是默认值。
    • 您的网站暂时无法访问:P
    • Rails doc 也有一些不错的 info on flash and flash.now
    【解决方案2】:

    我还建议在显示时清除 flash 内部哈希。 flash.clear 会以干净的方式解决问题:

          <% flash.each do |key, value| %>
           <div class="alert alert-<%= key %>">
            <%= value %>
           </div>
          <% end %>
          <% flash.clear %> #this line clears the object
    

    http://api.rubyonrails.org/classes/ActionDispatch/Flash/FlashHash.html#method-i-clear

    【讨论】:

      【解决方案3】:

      以前我有同样的问题,但现在通过这个解决了:
      在你的代码中试试这个

      <script type="text/javascript">
        $(document).ready(function(){
          setTimeout(function(){
          $('#flash').fadeOut();
          }, 2000);
        })
      </script>
      

      【讨论】:

        【解决方案4】:

        另一种方法是在部分末尾使用flash.clear,如下所示:

        <% if !flash.empty? %>
          <div class="flash-messages-container">
            <% flash.each do |name, msg| %>
              <% if msg.is_a?(String) && [:success, :info, :error, :warning].include?(name) %>
                <div class="flash-message" data-type="<%= name %>" >
                  <%= msg %>
                </div>
              <% end %>
            <% end %>
          </div>
          <% flash.clear %>
        <% end %>
        

        【讨论】:

        • flash.clear 比 干净得多
        • 根据我的经验,这是最好的方法,除非您需要保留 Flash 消息。我不知道为什么模板(也许脚手架可以?)默认情况下不附带 flash.clear。
        • flash.clear 不适合我。 Flash 仍然没有消失
        【解决方案5】:

        即使这样也不起作用......某些类型的异常,如语法错误......将阻止任何类型的 cookie、flash 或参数从控制器传输到视图。您唯一的选择是使用会话密钥,然后在显示错误后将其清除。

        尝试使用语法错误的解决方案...您应该会看到您的消息不会出现在重定向页面中,除了会话密钥之外的任何其他内容.....

        【讨论】:

          【解决方案6】:

          在 flash.now 和常规 flash 之间做出决定是一件令人头疼的事情,而且在我的经验中非常脆弱。我使用普通的 flash,然后修改我的部分显示 flash 以在用户看到它后删除每个 flash 的内容。我认为这更好,因为

          a) 你不必考虑它

          b) “用户看到了吗?” (即“flash 是否已部分渲染出来?”)是决定是否清除 flash 的最佳标准,而不是您应用中的任何逻辑。

          我的 flash 部分看起来像这样 - 我还使用了一些 jquery 来突出显示闪光灯(即让它们闪烁黄色一秒钟):

          <div id="flashes">
          
            <% if flash[:notice] %>
              <p id="flash_notice" class="messages notice"><%= flash[:notice] %></p>
              <%= javascript_tag "$('#flash_notice').effect('highlight',{},1000);" %>
            <% end %>
          
            <% if flash[:error] || flash[:errors] %>
              <p id="flash_errors" class="messages errors"><%= flash[:error] || flash[:errors] %></p>
              <%= javascript_tag "$('#flash_errors').effect('highlight',{},1000);" %>
            <% end %>
          
            <% flash[:error] = flash[:errors] = flash[:notice] = nil %>
          </div>
          

          【讨论】:

          • 也许是个愚蠢的问题,但这样做会不会删除另一个即将渲染的 flash?
          • @Bradford - 我不这么认为:我只是清除了我渲染的三个闪存键。没有被渲染它们就不可能被清除,并且在它们被渲染和清除之间不会发生任何事情,因为清除发生在同一个部分。
          • 游戏迟到了,但我喜欢这个。
          • 我刚试过这个。我认为 flash.discard(a_single_key) 更好,因为将值设置为 nil 不会从闪存中删除密钥。因此,它可能会为已删除的键显示一条空消息。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-23
          • 1970-01-01
          • 1970-01-01
          • 2012-08-12
          相关资源
          最近更新 更多