当会话被销毁时我的应用程序可以收到通知吗?如果是这样,怎么办?
我的基于 Devise/Warden 的应用程序将模型 ID 存储在 session[] 变量中。我想在 session[] 被销毁时销毁该对象。
- 当会话被销毁时,是否有回调或某种机制来通知我的应用程序?
- 该机制是否可靠,或者我应该运行夜间清理脚本来清理任何孤立的对象?
为了清楚起见,这里是我的控制器代码的片段:
class WizardsController < ApplicationController
before_filter :find_or_create_wizard
...
private
def find_or_create_wizard
@wizard = Wizard.find_by_id(session[:wizard_id]) || Wizard.create.tap {|w| session[:wizard_id] = w }
end
end
重申一下问题:我应该如何以及何时销毁向导对象?
My Devise/Warden-based app stores a model ID in the session[] variable. I want destroy the object when the session[] is destroyed.
- Is there a callback or some mechanism to notify my app when the session is destroyed?
- Is the mechanism dependable, or should I run a nightly cleanup script to vacuum up any orphaned objects?
To make it clear, here's a snippet of my controller code:
class WizardsController < ApplicationController
before_filter :find_or_create_wizard
...
private
def find_or_create_wizard
@wizard = Wizard.find_by_id(session[:wizard_id]) || Wizard.create.tap {|w| session[:wizard_id] = w }
end
end
To restate the question: how and when should I destroy the Wizard object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Warden::Hooks https://github.com/hassox /warden/blob/master/lib/warden/hooks.rb 在注销或身份验证后执行操作。
Use the Warden::Hooks https://github.com/hassox/warden/blob/master/lib/warden/hooks.rb to do things after sign out or authentication.
会话是指用户退出时吗?
尝试猴子修补
application_controller.rb
中的sign_out
方法您可以在
lib/devise/controllers/helpers.rb
中找到相关的 Gem 代码。每当用户通过名为
expire_session_data_after_sign_in!
的函数登录或注册时,会话数据也会被清除。 ,也可以覆盖它:By session, do you mean when the user logs out?
Try monkey patching the
sign_out
method in yourapplication_controller.rb
You can find the relevant Gem code in
lib/devise/controllers/helpers.rb
Session data is also cleared whenever a user signs in or signs up via a function called
expire_session_data_after_sign_in!
, could override that too: