我有一个Rails4应用程序,当会话由于不活动而超时时,我试图刷新自定义消息。
我是通过配置timeout_in
中的devise.rb
设置来实现此目的的:
Devise.setup do |config|
...
config.timeout_in = 30.minutes
...
end
并向我的应用程序控制器添加自定义救援:
rescue_from CanCan::AccessDenied do |exception|
if user_signed_in?
flash.now.alert = exception.message
render text: '', layout: true, status: 403
else
redirect_to new_user_session_path, notice: flash[:alert] || "You must login first"
end
end
一切似乎都运行良好......当会话超时时,flash[:alert]
已经有正确的消息,所以我只是使用它,当用户尝试访问资源而不先登录然后&# 34;你必须先登录"消息被返回。
这是呈现这些警报的main.html.haml页面中的代码:
.container
.main-content
#flash
- flash.each do |type, msg|
%div{class: ('alert alert-dismissable fade in')}
%button.close{data: {dismiss: :alert}} ×
= msg
= yield
问题在于,我偶尔会看到带有文字的闪光灯" True"出现在会话超时消息的正下方:
我无法弄清楚它的来源。似乎有些东西正在创建具有该值的flash消息。我想知道我做错了什么或者是否有更好的方法来显示会话超时消息。有什么想法吗?
答案 0 :(得分:5)
我知道这是一个迟到的回复,但这就是我所做的。
flash.each do |type, message|
unless type.to_s == 'timedout'
<!-- Carry on printing messages -->
答案 1 :(得分:1)
尝试输出每个闪存的type
- 也许Devise或其他东西正在使用闪存而不仅仅是消息。 Read more about using the flash for other things than messages.
我不会遍历所有闪存键来渲染它们 - 我只显示专用于消息的那些(通常是:notice
和:alert
)。
答案 2 :(得分:0)