为什么我多次收到此错误?

Why am I receiving this error multiple times?

我正在运行一个$rails测试命令来查看是否一切正常,但我收到了这个:

ERROR["test_email_addresses_should_be_unique", #<Minitest::Reporters::Suite:0x00007f2670deadd0 @name="UserTest">, 0.01948296799992022]
 test_email_addresses_should_be_unique#UserTest (0.02s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_password_should_be_present_(nonblank)", #<Minitest::Reporters::Suite:0x00005586062f6a20 @name="UserTest">, 0.039645324000048277]
 test_password_should_be_present_(nonblank)#UserTest (0.04s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_password_should_have_a_minimum_length", #<Minitest::Reporters::Suite:0x0000558606331508 @name="UserTest">, 0.05958008599986897]
 test_password_should_have_a_minimum_length#UserTest (0.06s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_email_validation_should_reject_invalid_addresses", #<Minitest::Reporters::Suite:0x0000558606364958 @name="UserTest">, 0.07700808699996742]
 test_email_validation_should_reject_invalid_addresses#UserTest (0.08s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_should_redirect_edit_when_logged_in_as_wrong_user", #<Minitest::Reporters::Suite:0x00007f2670e5ea50 @name="UsersControllerTest">, 0.11646463199986101]
 test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (0.12s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_should_redirect_update_when_not_logged_in", #<Minitest::Reporters::Suite:0x00007f2670eae8e8 @name="UsersControllerTest">, 0.12786450899989177]
 test_should_redirect_update_when_not_logged_in#UsersControllerTest (0.13s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_should_redirect_edit_when_not_logged_in", #<Minitest::Reporters::Suite:0x00005586063af020 @name="UsersControllerTest">, 0.14731222199998228]
 test_should_redirect_edit_when_not_logged_in#UsersControllerTest (0.15s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_should_get_new", #<Minitest::Reporters::Suite:0x00007f2670f26ac8 @name="UsersControllerTest">, 0.16310770199993385]
 test_should_get_new#UsersControllerTest (0.16s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_should_redirect_destroy_when_not_logged_in", #<Minitest::Reporters::Suite:0x00005586063c6478 @name="UsersControllerTest">, 0.17951116699987324]
 test_should_redirect_destroy_when_not_logged_in#UsersControllerTest (0.18s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_should_redirect_followers_when_not_logged_in", #<Minitest::Reporters::Suite:0x000055860646fd70 @name="UsersControllerTest">, 0.1943119969998861]
 test_should_redirect_followers_when_not_logged_in#UsersControllerTest (0.19s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_should_redirect_index_when_not_logged_in", #<Minitest::Reporters::Suite:0x00005586065155e0 @name="UsersControllerTest">, 0.21003705899988745]
 test_should_redirect_index_when_not_logged_in#UsersControllerTest (0.21s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_should_redirect_update_when_logged_in_as_wrong_user", #<Minitest::Reporters::Suite:0x00005586065cc038 @name="UsersControllerTest">, 0.2277173119998679]
 test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (0.23s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_should_redirect_destroy_when_logged_in_as_a_non-admin", #<Minitest::Reporters::Suite:0x00007f2670f73760 @name="UsersControllerTest">, 0.24164153399988209]
 test_should_redirect_destroy_when_logged_in_as_a_non-admin#UsersControllerTest (0.24s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_should_redirect_following_when_not_logged_in", #<Minitest::Reporters::Suite:0x00007f2670fbf5e8 @name="UsersControllerTest">, 0.25870832299983704]
 test_should_redirect_following_when_not_logged_in#UsersControllerTest (0.26s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_should_get_contact", #<Minitest::Reporters::Suite:0x00007f267100acc8 @name="StaticPagesControllerTest">, 0.2745016510000369]
 test_should_get_contact#StaticPagesControllerTest (0.27s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_should_get_home", #<Minitest::Reporters::Suite:0x0000558606717348 @name="StaticPagesControllerTest">, 0.2904469089999111]
 test_should_get_home#StaticPagesControllerTest (0.29s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_should_get_about", #<Minitest::Reporters::Suite:0x00005586067deee8 @name="StaticPagesControllerTest">, 0.3036177300000418]
 test_should_get_about#StaticPagesControllerTest (0.30s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_should_get_help", #<Minitest::Reporters::Suite:0x00007f26701107b8 @name="StaticPagesControllerTest">, 0.34408128299992313]
 test_should_get_help#StaticPagesControllerTest (0.34s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

ERROR["test_layout_links", #<Minitest::Reporters::Suite:0x00007f26701da1d0 @name="SiteLayoutTest">, 0.35210771399988516]
 test_layout_links#SiteLayoutTest (0.35s)
NoMethodError:         NoMethodError: undefined method `digest' for #<Class:0x00007f2670dcdca8>
            test/fixtures/users.yml:4:in `get_binding'

  19/19: [==============================================================================================================================================================================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.35965s
19 tests, 0 assertions, 0 failures, 19 errors, 0 skips
ubuntu:~/environment/sample_app (master) $ 

这就是我的 users.yml 文件的样子。该错误似乎指向完全相同的行。抱歉,如果这很明显,我只是在学习教程,之前从未接触过 Rails。

michael:
  name: Michael Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>
  admin: true
  activated: true
  activated_at: <%= Time.zone.now %>

archer:
  name: Sterling Archer
  email: duchess@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

lana:
  name: Lana Kane
  email: hands@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

malory:
  name: Malory Archer
  email: boss@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

<% 30.times do |n| %>
user_<%= n %>:
  name:  <%= "User #{n}" %>
  email: <%= "user-#{n}@example.com" %>
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>
<% end %>

这是第 4 行('get_binding' 是什么意思?)

  password_digest: <%= User.digest('password') %>

更新 这是我的 user.rb 文件:

class User < ApplicationRecord
  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: true
  has_secure_password
end

您在 app/models/user.rb 中的用户模型应该有一个 digest 方法,但没有。

您的用户 has_secure_password 为用户添加了一些额外的方法。有关文档,请参阅 https://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html。但不是 User.digest

也许教程中有一部分您跳过了该方法的创建?或者教程有误或已过时。

无论如何,在您的 users.yml 文件中,您可以使用 password_digest: BCrypt::Password.create('Your_Password')) 为每个用户设置密码,而不是 User.digest 方法。

请参阅 Encrypt User's password in seed file 以了解有相同根本问题的人。