我想在用户拒绝 EULA 条款时发送flash[:notice]
。所以它断开了他的连接并重定向到after_sign_out_path_for
(这是我的root_path
这是我的public#index
(。但是当我们到达页面时,似乎没有任何flash[:notice]
丢失。
这是我的代码:
disclaimer_controller.rb
class DisclaimerController < ApplicationController
def refuse
@user = User.find(params[:id])
@user.update_column('accept_disclaimer', false)
@user.update_column('sign_in_count', 0)
@user.save
@user.reload
sign_out
flash[:notice] = "test
redirect_to root_path
end
公共.html.erb
<div class="main">
<%= render_flash_messages %>
<div class="main-inner">
<%= yield %>
</div><!-- /main-inner -->
</div><!-- /main -->
application_helper.rb(与我的布局应用程序和公共一起使用(
def render_flash_messages
render partial: "layouts/flash_messages"
end
_flash_messages.html.erb
<div class="show-notif">
<% if notice.present? %>
<div class="flash-message alert alert-success collapse in">
<%= notice %>
<button type="button" class="close" data-dismiss="alert"><i class="icon-remove"></i> <%= I18n.t('helpers.link.close') %></button>
</div>
<% end %>
</div>
我测试了我的部分 _flash_messages.html.erb,当有通知时(即使在我的公共布局中(,它也能完美运行。
所以我试图用root_url
、root_path
、public_index_path
和new_user_session_path
来改变路径,但没有任何效果。
我还尝试了不同的编码合成器,因为:
-
flash[notice] = "test" then redirect_to root_path
-
redirect_to root_path, :notice => "test"
-
redirect_to root_path, notice: "test"
-
redirect_to root_path, flash
-
redirect_to root_path, :flash => { :error => "Insufficient rights!" }
-
redirect_to root_path, flash: { :error => "Insufficient rights!" }
-
redirect_to(root_path, {:flash => { :error => "Insufficient rights!" }})
(每次我尝试之前列出的所有不同路径时(
我也尝试使用flash.keep
或flash.keep(:notice)
(我使用了Rails版本3.2.13(,没有任何变化。
最后,我尝试像这样改变我的路线:
- root :to => redirect { |p, req| req.flash.keep;"公共#索引" }而不是
- root to: "public#index">
我想注意到我在after_sign_in_path_for
上遇到了几乎相同的问题,我找到了解决方案。问题是flash[:notice]
正在发送控制器,而不是在此控制器的重定向中发送(即使有flash.keep
(。所以我只是放置直接路径而不是after_sign_in_path_for,效果很好。但在这里,事实并非如此。
我做错了什么?
我最终得到了一个解决方案。
我在Disclaimer#refuse
中设置了一个cookie,重定向后cookie仍然存在,所以我测试我的cookie是否存在,如果存在,我测试其值并在Sessions#new 中设置闪存消息
设置闪光消息后,我删除了cookie。
disclaimer_controller.rb
def refuse
@user = User.find(params[:id])
@user.update_column('accept_disclaimer', false)
@user.update_column('sign_in_count', 0)
@user.save
sign_out
cookies[:login] = { :value => "refuse", :expires => Time.now + 10} # just to secure
redirect_to root_path
end
sessions_controller.rb
def new
if cookies[:login]
if cookies[:login] == "refuse"
flash[:alert] = "You have to accept the EULA terms to use our application."
elsif cookies[:login] == "signout"
flash[:notice] = "Sign out successfully"
end
end
cookies.delete(:login)
super
end
它工作得很好。但是,我没有找到任何解决方案来在未登录用户到达root_path时保持我的闪存(从重定向(。我不明白为什么。所以,如果有人能对这个问题有所了解,我真的很感兴趣。