取消删除acts_as_paranoid在设计登录时删除用户

我有一个Rails 3.1.3应用程序,它使用devise进行用户身份validation,并使用acts_as_paranoid对其进行软删除。 我希望在密码重新创建,用户注册和用户登录时取消删除帐户,因此如果他们提供已删除的电子邮件,我会抓取该帐户,重新启用该帐户,然后继续操作(密码重新创建或登录) )。

但是在Users::SessionsController#create #create操作中,取消删除用户后会收到Unauthorized错误(但用户现在应该可见)。 代码是:

 def create # Take into account acts_as_paranoid deleted users resource = resource_class.only_deleted.find_by_email(params[resource_name][:email]) resource.undelete! if resource resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new") set_flash_message(:notice, :signed_in) if is_navigational_format? sign_in(resource_name, resource) respond_with resource, :location => after_sign_in_path_for(resource) end 

如果我在取消删除后添加一个resource.reload调用它不会改变任何东西。 如果我再次登录,用户通常会登录,因为它在之前的尝试中未被删除。

为什么会这样? 如何在一次create调用中取消删除并登录?

解决了以下代码片段:

 def create # Take into account acts_as_paranoid deleted users if (resource = resource_class.only_deleted.find_by_email(params[resource_name][:email])) resource.undelete! # Copied from Warden::Strategies database_authenticatable: sign_in resource if resource.valid_password?(params[resource_name][:password]) end super end 
Interesting Posts