0

当我尝试调用“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
4

2 回答 2

1

这是旧的,但您可能有两个模型(在 Warden 的案例中是范围):一个用于 User,另一个用于 AdminUser。您可以只对所需的模型/范围使用 if 语句:

Warden::Manager.before_logout do |user, auth, opts|
    if opts[:scope] == :user
       user.current_sign_in_at = nil
       user.save!
    end
end
于 2015-09-01T20:59:01.387 回答
-2

通过在 config/initializers/devise.rb 中添加它来解决它。为您想在注销时执行的操作添加代码。

Warden::Manager.before_logout do |user,auth,opts|
  # store what ever you want on logout
  # If not in initializer it will generate two records (strange)
end
于 2013-07-15T19:26:25.300 回答