Ruby-on-rails 为什么闪信不会消失?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4613952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:01:41  来源:igfitidea点击:

Why flash message won't disappear?

ruby-on-rails

提问by Jimmy Huang

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.

我正在我的控制器中进行一些异常处理,当在 :create 操作中抛出异常时,我将呈现给 :new 操作并显示一条 flash 消息。

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.

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

Anyone who knows what is the reason?

有谁知道是什么原因?

My controller code:

我的控制器代码:

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

My layout code (Haml):

我的布局代码(Haml):

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

回答by Zabba

Replace

代替

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

with

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

and see if that is the result you expect?

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

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

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

回答by Max Williams

Deciding between flash.now and regular flash is a pain in the ass and quite fragile in my experience. I use regular flash and then modify my partial which displays the flashes to delete the contents of each flash after the user has seen it. I think this is better because

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

a) you don't have to think about it

a) 你不必考虑它

b) "has the user seen it?" (ie "has the flashes partial been rendered out?") is the best criterion for deciding whether or not to clear the flash, rather than any logic in your app.

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

My flash partial looks like this - i also use a bit of jquery just to highlight the flashes (ie make them flash yellow for a second):

我的闪光部分看起来像这样 - 我还使用了一些 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>

回答by CoderDave

An alternative is to use flash.clearat the end of the partial like so:

另一种方法是在部分末尾使用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 %>

回答by Nimish

Previously I had same problem,But now solved through this:
Try this in your code

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

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

回答by thomas dh

I also suggest to clear the flashinner hashes upon displaying. flash.clearwill do the trick in a clean way :

我还建议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

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

回答by surtep

Even this does not work......certain types of exceptions like syntax errors...will prevent any type of cookie, flash or parameters from being transferred from controller to view. Your only option is to use a session key and then clear it after showing the error.

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

Try your solution with a syntax error...you should see that your message won't appear in the redirected page with anything else except with a session key.....

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