ruby-on-rails-4 - 设计保存用户导致reset_password_token设置为nil

ruby-on-rails-4 - 设计保存用户导致reset_password_token设置为nil,第1张

这刚刚开始发生,我不确定何时或是什么导致它,但出于某种原因设计是在保存时清除我的reset_password_token。

为了调试我的问题,我实施了

  def reset_password_token=(token)
    debugger
    self[:reset_password_token] = token
  end

在我的User对象上,以便我可以看到谁在设置它。

每当我执行user.save()时,我的reset_password_token设置为nil,这里是callstack:

--> #0  User.reset_password_token=(token#NilClass) at /Users/joelnylund/Projects/myproject/adv/app/models/user.rb:125
    #1  Devise::Models::Recoverable.clear_reset_password_token at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.5.1/lib/devise/models/recoverable.rb:97
    #2  block (2 levels) in <module:Recoverable> at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.5.1/lib/devise/models/recoverable.rb:36
    ͱ-- #3  BasicObject.instance_exec(*args) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:446
    #4  block in ActiveSupport::Callbacks::Callback.make_lambda(filter#Proc) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:446
    ͱ-- #5  Proc.call(*args) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:164
    #6  block in #<Class:ActiveSupport::Callbacks::Filters::Before>.halting(callback_sequence#ActiveSupport::Callbacks::CallbackSequence, user_callback#Proc, halted_lambda#Proc, filter#Proc) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:164
    ͱ-- #7  Proc.call(*args) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:504
    #8  block in ActiveSupport::Callbacks::CallbackSequence.call(*args#Array) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:504
    ͱ-- #9  Array.each at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:504
    #10 ActiveSupport::Callbacks::CallbackSequence.call(*args#Array) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:504
    #11 ActiveSupport::Callbacks._run_callbacks(callbacks#ActiveSupport::Callbacks::CallbackChain, &block#Proc) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:92
    #12 ActiveRecord::Base._run_save_callbacks(&block#Proc) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:776
    #13 ActiveRecord::Callbacks.create_or_update at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.2.1/lib/active_record/callbacks.rb:302
    #14 ActiveRecord::Persistence.save(*args) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.2.1/lib/active_record/persistence.rb:120
    #15 ActiveRecord::Validations.save(*args) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.2.1/lib/active_record/validations.rb:37
    #16 ActiveRecord::AttributeMethods::Dirty.save(*args) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.2.1/lib/active_record/attribute_methods/dirty.rb:21
    #17 block (2 levels) in ActiveRecord::Transactions.save(*args) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.2.1/lib/active_record/transactions.rb:286
    #18 block in ActiveRecord::Transactions.with_transaction_returning_status at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.2.1/lib/active_record/transactions.rb:347
    #19 block in ActiveRecord::ConnectionAdapters::DatabaseStatements.transaction(options#Hash) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213
    #20 ActiveRecord::ConnectionAdapters::TransactionManager.within_new_transaction(options#Hash) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/transaction.rb:188
    #21 ActiveRecord::ConnectionAdapters::DatabaseStatements.transaction(options#Hash) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213
    #22 ActiveRecord::Transactions::ClassMethods.transaction(options#Hash, &block#Proc) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.2.1/lib/active_record/transactions.rb:220
    #23 ActiveRecord::Transactions.with_transaction_returning_status at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.2.1/lib/active_record/transactions.rb:344
    #24 block in ActiveRecord::Transactions.save(*args) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.2.1/lib/active_record/transactions.rb:286
    #25 ActiveRecord::Transactions.rollback_active_record_state! at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.2.1/lib/active_record/transactions.rb:301
    #26 ActiveRecord::Transactions.save(*args) at /Users/joelnylund/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.2.1/lib/active_record/transactions.rb:285
    #27 Admin::UsersController.create at /Users/joelnylund/Projects/myproject/adv/app/controllers/admin/users_controller.rb:88

在设计中我看到了:

 if email_changed? || encrypted_password_changed?
        clear_reset_password_token
 end

如果是这样,我应该如何设置令牌并同时创建用户。我找到的工作是在保存之后设置令牌,这似乎有效,但不确定为什么这才刚开始发生..

最佳答案:

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。似乎唯一的解决方案是在设置reset_password_token

之前保存用户
user = User.new
user.email = 'test@example.com'
user.save # save
user.reset_password_token = 'token'
user.reset_password_sent_at = Time.now.utc
user.save # save again

答案 1 :(得分:1)

刚刚处理了这个并来到这里寻求建议。经过一些调整和测试后,我的解决方案只是在对象上执行保存以清除_changed?登录。我在after_create中为用户执行此操作:

def email_credentials
    self.save
    self.send_reset_password_instructions
end
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复