当我尝试调用“user.my_method”时,我正在努力弄清楚为什么我对 Warden::Manager.before_logout 的调用会为 NilClass 抛出 NoMethodError。然后我在 before_logout 块中添加了一个 debug puts 调用,发现它在每次注销时都被调用了两次——第一次是用户为 nil,然后紧接着是提供了用户对象。因此,我可以通过将调用更改为“user.my_method if user”来巧妙地绕过异常,但我仍然对不知道为什么 before_logout 被调用两次感到不舒服。有没有其他人看过这个?这可能是那些仅发生在开发中的环境异常之一吗?
Devise.setup do |config|
Warden::Manager.after_authentication do |user,auth,opts|
user.update_attributes(is_active: true)
end
Warden::Manager.before_logout do |user,auth,opts|
user.update_attributes(is_active: false) if user
end