Ruby on Rails:用户的未定义方法'new_token'

问题描述 投票:3回答:1

我正在关注Michael Hartl的Ruby on Rails教程,但遇到了我无法弄清的错误。

我一直收到以下错误,NoMethodError:用户的未定义方法'new_token'。当我只是尝试创建User的实例,或者我只是从控制台调用User时,就会发生这种情况。

下面的代码被精简了,因为我试图通过注释掉其他一些方法来隔离问题,这是>]

    class User < ActiveRecord::Base
      attr_accessor :remember_token, :activation_token, :reset_token

      validates :name, presence: true, length: {maximum: 50}
      validates :email, presence: true, length: {maximum: 255}, format: {with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i}, uniqueness: {case_sensitive: false}
      validates :password, presence: true, length: {minimum: 6}, allow_nil: true
      has_secure_password

      def User.digest(string)
        cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
        BCrypt::Password.create(string, cost: cost)
       end
      def User.new_token
        SecureRandom.urlsafe_base64
      end
   end

[当我尝试在控制台中做一些简单的事情,例如返回第一个用户时,出现此错误:

2.2.0 :019 > User.first
NoMethodError: undefined method `new_token' for User (call 'User.connection' to establish a connection):Class
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/activerecord-4.2.0/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
    from /Users/Jshyu/Desktop/Jitter/app/models/user.rb:15:in `<class:User>'
    from /Users/Jshyu/Desktop/Jitter/app/models/user.rb:2:in `<top (required)>'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `load'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `block in load_file'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:647:in `new_constants_in'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:456:in `load_file'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:354:in `require_or_load'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:494:in `load_missing_constant'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:184:in `const_missing'
    from (irb):19
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /Users/Jshyu/Desktop/Jitter/bin/rails:8:in `<top (required)>'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
    from /Users/Jshyu/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/Jshyu/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'2.2.0 :020 >

编辑:

更奇怪的是,当我注释掉类方法self.new_token时,我收到另一个错误,NameError:未定义的局部变量或用户的方法'']

class User < ActiveRecord::Base
  attr_accessor :remember_token, :activation_token, :reset_token

  validates :name, presence: true, length: {maximum: 50}
  validates :email, presence: true, length: {maximum: 255}, format: {with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i}, uniqueness: {case_sensitive: false}
  validates :password, presence: true, length: {minimum: 6}, allow_nil: true
  has_secure_password
  #Returns the hash digest of the given string
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end
#   def self.new_token
#     SecureRandom.urlsafe_base64
#   end
end

2.2.0 :026 > User
NameError: undefined local variable or method `  ' for User (call 'User.connection' to establish a connection):Class
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/activerecord-4.2.0/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
    from /Users/Jshyu/Desktop/Jitter/app/models/user.rb:22:in `<class:User>'
    from /Users/Jshyu/Desktop/Jitter/app/models/user.rb:2:in `<top (required)>'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `load'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `block in load_file'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:647:in `new_constants_in'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:456:in `load_file'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:354:in `require_or_load'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:494:in `load_missing_constant'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:184:in `const_missing'
    from (irb):26
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /Users/Jshyu/Desktop/Jitter/bin/rails:8:in `<top (required)>'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
    from /Users/Jshyu/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/Jshyu/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'2.2.0 :027 > 

我最近安装了sublimelinter和rubocop,但是自从将它们删除后...

我正在关注Michael Hartl的Ruby on Rails教程,但遇到了我无法弄清的错误。我不断收到以下错误,NoMethodError:用户的未定义方法'new_token'。这个...

退出rails控制台并重新启动服务器。再试一次!

ruby-on-rails ruby sublimetext3
1个回答
-1
投票

退出rails控制台并重新启动服务器。再试一次!

© www.soinside.com 2019 - 2024. All rights reserved.