1

登录后,如果我转到 /users/edit 页面,我会收到 Flash 消息“您已成功登录”。

在注册控制器中,我尝试了:

flash.now  # either this or below one
flash.discard

同样,我尝试过类似的布局

    <% if flash[:alert] || flash[:error] || flash[:notice] %>
       <%= content_tag :div, :class => "alert alert-info info-inside" do -%>
          <button class="close" data-dismiss="alert">×</button>
          <%= flash.now[:alert] if flash[:alert] %>
          <%= flash.now[:error] if flash[:error] %>
          <%= flash.now[:notice] if flash[:notice] %>
       <% end -%>
    <% end %>

此外,如果我创建产品并进入其他页面,它会显示错误的 Flash 消息。刷新时它会消失。

请告诉我如何避免此类闪现消息。我在我的布局文件和需要的控制器中尝试了 flash.now 和 flash.discard。但这对我没有帮助。

4

1 回答 1

0

试试下面的代码。它检查是否确实存在闪存消息,然后获取成功消息和错误消息。

<% if flash.present? %>
<div id="flash" class="flash">
<% flash.each do |name, msg| %>
  <div class="alert alert-<%= name == :notice ? "success" : "error" %>">
    <%= content_tag :div, msg, id: "flash_#{name}" if msg.is_a?(String) %>
  </div>
<% end %>

于 2013-03-29T13:47:53.337 回答