在Rails 4.2.6升级设计时未定义的方法`reset_password_sent_at

时间:2016-10-24 17:42:14

标签: ruby-on-rails devise

我将Rails版本从3.2升级到4.2.6,并将版本从1.4.9升级到4.2.0。当我使用创建更新密码运行我的应用程序时,一切都很好。但是当我将旧的生产数据库转储上传到新的转储并尝试重置密码时,我遇到了这个错误:

  

NoMethodError(未定义的方法`reset_password_sent_at =' for

     

你的意思是? reset_password_token_was):activemodel(4.2.6)lib / active_model / attribute_methods.rb:433:in

     

method_missing' devise (4.2.0) lib/devise/models/recoverable.rb:87:in set_reset_password_token'
  设计(4.2.0)lib / devise / models / recoverable.rb:45:in   send_reset_password_instructions' devise (4.2.0) lib/devise/models/recoverable.rb:119:in send_reset_password_instructions'设计(4.2.0)   app / controllers / devise / passwords_controller.rb:13:in create'
actionpack (4.2.6) lib/action_controller/metal/implicit_render.rb:4:in
send_action' actionpack(4.2.6)   lib / abstract_controller / base.rb:198:在process_action' actionpack (4.2.6) lib/action_controller/metal/rendering.rb:10:in process_action' actionpack(4.2.6)   lib / abstract_controller / callbacks.rb:20:in block in process_action'
activesupport (4.2.6) lib/active_support/callbacks.rb:117:in
call'
  activesupport(4.2.6)lib / active_support / callbacks.rb:555:in block (2 levels) in compile' activesupport (4.2.6) lib/active_support/callbacks.rb:505:in call' activesupport(4.2.6)   lib / active_support / callbacks.rb:92:在__run_callbacks__'
activesupport (4.2.6) lib/active_support/callbacks.rb:778:in
_ run_process_action_callbacks' activesupport(4.2.6)   lib / active_support / callbacks.rb:81:in run_callbacks' actionpack (4.2.6) lib/abstract_controller/callbacks.rb:19:in process_action'
  actionpack(4.2.6)lib / action_controller / metal / rescue.rb:29:in   process_action' actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:32:in阻止   process_action' activesupport(4.2.6)   lib / active_support / notifications.rb:164:in block in instrument'
activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in
instrument'
  activesupport(4.2.6)lib / active_support / notifications.rb:164:in   instrument' actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:30:in process_action'   actionpack(4.2.6)   lib / action_controller / metal / params_wrapper.rb:250:在process_action' activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:18:in process_action' actionpack(4.2.6)   lib / abstract_controller / base.rb:137:在process' actionview (4.2.6) lib/action_view/rendering.rb:30:in进程' actionpack(4.2.6)   lib / action_controller / metal.rb:196:在dispatch' actionpack (4.2.6) lib/action_controller/metal/rack_delegation.rb:13:in发送'
  actionpack(4.2.6)lib / action_controller / metal.rb:237:在block in action' actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:74:in发送' ActionPack的   (4.2.6)lib / action_dispatch / routing / route_set.rb:43:在serve'
actionpack (4.2.6) lib/action_dispatch/routing/mapper.rb:49:in
发送'   actionpack(4.2.6)lib / action_dispatch / journey / router.rb:43:block in serve' actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in每个' ActionPack的   (4.2.6)lib / action_dispatch / journey / router.rb:30:在serve'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:817:in
电话' warden(1.2.6)lib / warden / manager.rb:35:in block in call'
warden (1.2.6) lib/warden/manager.rb:34:in
catch'监狱长(1.2.6)   lib / warden / manager.rb:34:in call' client_side_validations (4.2.5) lib/client_side_validations/middleware.rb:15:in来电'机架(1.6.4)   lib / rack / etag.rb:24:in call' rack (1.6.4) lib/rack/conditionalget.rb:38:in来电'机架(1.6.4)   lib / rack / head.rb:13:in call' remotipart (1.2.1) lib/remotipart/middleware.rb:27:in来电' actionpack(4.2.6)   lib / action_dispatch / middleware / params_parser.rb:27:in call'
actionpack (4.2.6) lib/action_dispatch/middleware/flash.rb:260:in
来电' rack(1.6.4)lib / rack / session / abstract / id.rb:225:context' rack (1.6.4) lib/rack/session/abstract/id.rb:220:in来电'
  actionpack(4.2.6)lib / action_dispatch / middleware / cookies.rb:560:in   call' activerecord (4.2.6) lib/active_record/query_cache.rb:36:in 呼叫' activerecord(4.2.6)   LIB / active_record / connection_adapters /抽象/ connection_pool.rb:653:在   call' actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:29:in阻止通话'
  activesupport(4.2.6)lib / active_support / callbacks.rb:88:in   __run_callbacks__' activesupport (4.2.6) lib/active_support/callbacks.rb:778:in _ run_call_callbacks'
  activesupport(4.2.6)lib / active_support / callbacks.rb:81:in   run_callbacks' actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in呼叫' ActionPack的   (4.2.6)lib / action_dispatch / middleware / remote_ip.rb:78:in call'
actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in
来电'
  actionpack(4.2.6)   lib / action_dispatch / middleware / show_exceptions.rb:30:in call'
railties (4.2.6) lib/rails/rack/logger.rb:38:in
call_app' railties   (4.2.6)lib / rails / rack / logger.rb:20:在block in call' activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in块中标记为'
  activesupport(4.2.6)lib / active_support / tagged_logging.rb:26:in   tagged' activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in标记'铁路(4.2.6)   lib / rails / rack / logger.rb:20:in call' actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in来电'架   (1.6.4)lib / rack / methodoverride.rb:22:在call' rack (1.6.4) lib/rack/runtime.rb:18:in电话' activesupport(4.2.6)   LIB / active_support /缓存/策略/ local_cache_middleware.rb:28:在   call' rack (1.6.4) lib/rack/lock.rb:17:in呼叫' ActionPack的   (4.2.6)lib / action_dispatch / middleware / static.rb:120:在call' rack (1.6.4) lib/rack/sendfile.rb:113:in电话'铁路(4.2.6)   lib / rails / engine.rb:518:在call' railties (4.2.6) lib/rails/application.rb:165:in来电'机架(1.6.4)   lib / rack / content_length.rb:15:在pre_process'中的call' thin (1.5.1) lib/thin/connection.rb:81:in块中薄(1.5.1)   lib / thin / connection.rb:79:在catch' thin (1.5.1) lib/thin/connection.rb:79:in pre_process'薄(1.5.1)   lib / thin / connection.rb:54:in process' thin (1.5.1) lib/thin/connection.rb:39:in receive_data'事件机器(1.0.9.1)   lib / eventmachine.rb:193:在run_machine' eventmachine (1.0.9.1) lib/eventmachine.rb:193:in运行'薄(1.5.1)   lib / thin / backends / base.rb:63:in start' thin (1.5.1) lib/thin/server.rb:159:in start'机架(1.6.4)   lib / rack / handler / thin.rb:19:in run' rack (1.6.4) lib/rack/server.rb:286:in start'铁路(4.2.6)   lib / rails / commands / server.rb:80:在服务器的start' railties (4.2.6) lib/rails/commands/commands_tasks.rb:80:in块中'
  railties(4.2.6)lib / rails / commands / commands_tasks.rb:75:in tap'
railties (4.2.6) lib/rails/commands/commands_tasks.rb:75:in
服务器'
  railties(4.2.6)lib / rails / commands / commands_tasks.rb:39:in   run_command!' railties (4.2.6) lib/rails/commands.rb:17:in' bin / rails:4:require' bin/rails:4:in'

分贝/迁移/ xxxxxxxx_devise_create_users.rb:

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.string :email,              :null => false, :default => ""
      t.string :encrypted_password, :null => false, :default => ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, :default => 0
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Encryptable
      # t.string :password_salt

      ## Confirmable
      t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at

      # Token authenticatable
      # t.string :authentication_token

      ## Invitable
      # t.string :invitation_token

      t.timestamps
    end
    add_index :users, :email,                :unique => true
    add_index :users, :confirmation_token,   :unique => true
    add_index :users, :reset_password_token, :unique => true
  end

  def self.down
    drop_table :users
  end
end

旧迁移文件:

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.confirmable
      t.recoverable
      t.rememberable
      t.trackable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both

      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :confirmation_token,   :unique => true
    add_index :users, :reset_password_token, :unique => true
    # add_index :users, :unlock_token,         :unique => true
  end

  def self.down
    drop_table :users
  end
end

请帮我解决问题。

2 个答案:

答案 0 :(得分:2)

这是在1.5版本附近添加的新专栏。

检查此链接以获取commint。 https://github.com/plataformatec/simple_form/wiki/Nested-Models

由于您来自旧版本,因此您的数据库没有新列可用。您可以运行迁移,应修复此特定问题。但是,由于你跳过三个主要版本,可能会有更多的重大变化。

Devise在文档方面做得很好。在更新版本之前,请检查重大更改。 https://github.com/plataformatec/devise/commit/7693173ecd7f9b4198fafabafa3bdced7fab16d3

<强>供参考:

我个人建议不要在大更新时跳转版本(例如1.x到2.x),让你从1.x跳到4.x.

在语义版本控制中,

  • 最后一位数通常只是补丁。如果你要升级 最后一位数(例如1.4.2至1.4.5)。
  • 第二个数字是微小的变化,可能会引入一些 弃用和一些重大变化取决于他们如何管理 他们的版本
  • 第一个数字始终是重大更新,涉及更大的变化, 你应该非常小心。

有关语义版本控制的更多信息。

https://github.com/plataformatec/devise/releases

答案 1 :(得分:1)

@Edmund Lee点是正确的,1.5和最新版本之间有区别。 由于在最旧版本中找不到reset_password_sent_at,因此会抛出错误。为此,您无需更改设计方面,只需尝试添加:

ALTER TABLE users ADD reset_password_sent_at datetime;

到您的脚本文件并运行(简单技巧),并且在添加/编辑用户时也没有问题。