Flash 消息在重新加载时仍然存在

Raj*_*shM 5 ruby-on-rails ruby-on-rails-4

我有如下所示的简单操作:

def edit_password
end

def update_password
  if @user.update(user_params)
    redirect_to @user, notice: "Password was successfully changed"
  else
    flash.now[:notice] = "Password not changed"
    render :edit_password
  end
end
Run Code Online (Sandbox Code Playgroud)

在我看来,我有:

<% if flash[:error] 
  <%=flash[:error] %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

Flash 消息显示正确(当密码未更改时)。但是当我重新加载页面时,消息仍然存在。我相信它应该在页面刷新时消失。我究竟做错了什么?我到处找,这看起来很简单,但我想不通。感谢任何帮助。

And*_*rea 1

我遇到了同样的问题,这就是我为解决该问题所做的事情。我在 action_controller 中创建了以下方法,并在需要时从其他控制器将其调用为 before_action 过滤器:

def clear_flash_messages
  flash[:notice] = nil
  flash['danger'] = nil
  flash['success'] = nil
  flash[:warning] = nil
end
Run Code Online (Sandbox Code Playgroud)

这将有效地清除任何过时的闪存消息,并允许您开始新的消息,无论您只是重新加载还是重定向。