Rails flash:通知不起作用

Mih*_*hai 5 ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

我有这个代码:

def create
  login(params[:email], params[:password])

  if current_user
    flash[:notice] = "Welcome back #{current_user.email}"
    return redirect_to_first_page
  else
    flash[:notice] = "Email or password is wrong. Try again !"
    redirect_to root_url
  end
end
Run Code Online (Sandbox Code Playgroud)

登录成功后,将设置闪存并重定向到第一页.这部分正在运作.第二部分未设置闪存通知消息.然后,当显示页面时,不会显示来自闪存的消息.有什么不同我试着拥有

return redirect_to root_url
Run Code Online (Sandbox Code Playgroud)

但没有任何东西仍然没有显示 在我的控制器中,我有一个像flash_notice这样的帮手,它只是返回flash [:notice].这是因为闪存在视图中始终为空,但在控制器中可访问.在视图中我只有一行:

<%= flash_notice %>
Run Code Online (Sandbox Code Playgroud)

我正在使用rails 3.1

Per*_*ich 7

Chris Drappier是正确的,闪存哈希仅适用于一个请求.您可以使用"调用"方法

flash.keep[:notice]="This message will persist"

就个人而言,我喜欢在需要时将闪光灯保留在参数中.血腥的细节在这里:

http://guides.rubyonrails.org/action_controller_overview.html#the-flash