jco*_*lum 3 ruby-on-rails ruby-on-rails-3
SO上有很多关于此的帖子(respond_with重定向通知flash消息不起作用 为什么:Rails 3中的重定向后通知没有显示,等等),我已经读过至少4但仍然无法解决这个问题.
我有一部分网站可以让人们在创建帐户之前做一些事情.从UX的角度来看,我更喜欢这个.所以他们被允许做X和Y然后他们被重定向到"创建帐户"页面(使用Devise).
重定向看起来像:
if userIsNew
... stow information in a cookie to be retrieved later ...
redirect_to "/flash", flash[:notice]
=> "Ok, we'll get right on that after you sign up (we need your email)."
and return # this has to be here, since I'm terminating the action early
end
Run Code Online (Sandbox Code Playgroud)
所以,"/flash"
是我提出来测试这一个简单的页面.它没有做任何事情,没有自己的标记,只有来自application.html的基本html,它在正文中有这一行:
<% if flash[:notice] %>
<p><%= notice %></p>
<% else %>
No notice!
<% end %>
Run Code Online (Sandbox Code Playgroud)
它每次都说'没有通知'.
我试过了:
before_filter
静态控制器中添加flash.keep给我:notice =>
而不是flash[:notice] =>
redirect_to :back
随着 flash[:notice] =>
Dam*_*ien 13
它也是
flash[:notice] = 'blablabla'
redirect_to foo_url
Run Code Online (Sandbox Code Playgroud)
要么
redirect_to foo_url, notice: 'blablabla'
我要ApplicationController#redirect_to
重新调用,flash.keep
以便任何消息都保留在重定向上,而不必显式调用flash.keep
我的控制器操作.到目前为止运作良好.尚未确定持有不需要的消息的方案.
class ApplicationController < ActionController::Base
def redirect_to(*args)
flash.keep
super
end
end
Run Code Online (Sandbox Code Playgroud)
如果有任何情况这不是一个好的解决方案,请告诉我.
一段时间以来,我一直在与同样的问题作斗争,没有一个帖子似乎有所帮助.事实证明 - 就像通常情况一样 - 问题出现在我的代码中.我确实有一个我忘了的"redirect_to",它正在清除闪光灯.
也就是说,我的"root_path"是由StaticPagesController的home方法提供的."home"正在进行一些检查,然后将您重定向到user_path.
在我的代码中,我有很多地方
redirect_to root_path, :flash => {error: @error}
Run Code Online (Sandbox Code Playgroud)
这些重定向从不显示闪存,因为我的隐藏的"home"控制器服务于"root_path"正在进行另一次重定向以清除闪存.
因此,当我在"home"控制器方法中添加"flash.keep"时,我的问题就解决了
def home
if current_user
@user = current_user
flash.keep
redirect_to @user unless @user.no_role?
end
end
Run Code Online (Sandbox Code Playgroud)