NameError: undefined local variable or method `rem

2019-08-13 03:27发布

问题:

My user.rb file consists of

class User < ActiveRecord::Base
  attr_accessor :remember_token
  before_save { self.email = email.downcase }
  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }

  has_secure_password 
  validates :password, presence: true, length: { minimum: 6 }

  # 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

  # Returns a random token.
  def User.new_token
    SecureRandom.urlsafe_base64
  end

  # Remembers a user in the database for use in persistent sessions.
  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
 end

  # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end

  # Returns true if the given token matches the digest.
  def authenticated?(remember_token)
    return false if remember_digest.nil?
    BCrypt::Password.new(remember_digest).is_password?(remember_token)
  end
end

i get the errors

ERROR["test_authenticated?_should_return_false_for_a_user_with_nil_digest", UserTest, 2015-06-01 21:01:42 +0000]
 test_authenticated?_should_return_false_for_a_user_with_nil_digest#UserTest (1433192502.88s)
NameError:         NameError: undefined local variable or method `remember_digest' for #<User:0x0000000788d0b8>
            app/models/user.rb:38:in `authenticated?'
            test/models/user_test.rb:70:in `block in <class:UserTest>'
        app/models/user.rb:38:in `authenticated?'
        test/models/user_test.rb:70:in `block in <class:UserTest>'



ERROR["test_login_with_valid_information_followed_by_logout", UsersLoginTest, 2015-06-01 21:01:43 +0000]
 test_login_with_valid_information_followed_by_logout#UsersLoginTest (1433192503.06s)
NoMethodError:         NoMethodError: undefined method `remember_digest=' for #<User:0x00000007ba19e8>
            app/models/user.rb:28:in `remember'
            app/helpers/sessions_helper.rb:9:in `remember'
            app/controllers/sessions_controller.rb:9:in `create'
            test/integration/users_login_test.rb:22:in `block in <class:UsersLoginTest>'
        app/models/user.rb:28:in `remember'
        app/helpers/sessions_helper.rb:9:in `remember'
        app/controllers/sessions_controller.rb:9:in `create'
        test/integration/users_login_test.rb:22:in `block in <class:UsersLoginTest>'

  22/22: [===================================================================================================================================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.38245s
22 tests, 42 assertions, 0 failures, 2 errors, 0 skips

回答1:

I had similar problems. It is most likely that you did not run the migrations at the beginning of the chapter.

you should be fine if you run:

$ rails generate migration add_remember_digest_to_users remember_digest:string 

followed by

$ bundle exec rake db:migrate