Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails NameError:未定义的局部变量或方法“记住摘要和命名错误”_Ruby On Rails_Ruby_Ruby On Rails 4_Error Handling - Fatal编程技术网

Ruby on rails NameError:未定义的局部变量或方法“记住摘要和命名错误”

Ruby on rails NameError:未定义的局部变量或方法“记住摘要和命名错误”,ruby-on-rails,ruby,ruby-on-rails-4,error-handling,Ruby On Rails,Ruby,Ruby On Rails 4,Error Handling,My user.rb文件由 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 va

My user.rb文件由

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
class用户
我得到了错误

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
ERROR[“test\u authenticated?”\u应该为用户返回\u false\u以及\u nil\u摘要”,UserTest,2015-06-01 21:01:42+0000]
test\u authenticated?\u应使用\u nil\u digest\UserTest(143192502.88s)为用户返回\u false\u
NameError:NameError:未定义的局部变量或方法“记住\u摘要”#
app/models/user.rb:38:in'authenticated'
测试/模型/用户测试。rb:70:in‘block in’
app/models/user.rb:38:in'authenticated'
测试/模型/用户测试。rb:70:in‘block in’
错误[“使用有效信息测试登录,然后退出”,UsersLoginTest,2015-06-01 21:01:43+0000]
使用有效信息测试登录,然后退出用户登录测试(143192503.06s)
NoMethodError:NoMethodError:for的未定义方法'membere\u digest='#
app/models/user.rb:28:in“记住”
app/helpers/sessions\u helper.rb:9:in“记住”
app/controllers/sessions\u controller.rb:9:in'create'
test/integration/users\u login\u test.rb:22:in'block in'
app/models/user.rb:28:in“记住”
app/helpers/sessions\u helper.rb:9:in“记住”
app/controllers/sessions\u controller.rb:9:in'create'
test/integration/users\u login\u test.rb:22:in'block in'
22/22:[===========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
以1.38245s完成
22次测试,42次断言,0次失败,2次错误,0次跳过

我也有类似的问题。很可能您没有在本章开头运行迁移

如果您跑步,您应该会很好:

$ rails generate migration add_remember_digest_to_users remember_digest:string 

$ bundle exec rake db:migrate

memory\u digest
是users表中的一个属性,对吗?该错误告诉您没有名为“memory\u digest”的用户类的实例方法。你希望有一个吗?它没有在你的类中定义。它是您的users表中的一列吗?是的,Rememory_digest是用户表的一个属性,我在创建后使用以下命令添加了该属性:$generate migration add_Rememory_digest_to_users Rememory_digest:stringCan your show us your schema.rb?@LannyBose该模式是ActiveRecord::schema.define(版本:2015061202545)创建_表“users”,force::cascade do | t | t.string“name”t.string“email”t.datetime“created_at”,null:false t.datetime“updated_at”,null:false t.string“password_digest”end我猜memory_digest没有更新。怎么做。。?结束