我正在尝试跟随Michael Hartl的Ruby on Rails书籍,并且现在在第10章,尝试允许用户编辑其个人资料设置,但是出现了最奇怪的错误-bcrypt的password_digest正在破坏我的测试。
users.yml
user:
name: Example User
email: example@gmail.com
password_digest: <%= User.digest("password") %>
当我在Rails控制台中运行这段代码时,我看到以下结果:
user.valid? => false
user.errors.full_messages => ["密码不能为空", "密码太短(最少为6个字符)"]
user.rb
class User < ApplicationRecord
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
before_save {
self.email.downcase!
}
validates :name, presence: true, length: { maximum: 50 }
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
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
end
我正在使用 bcrypt 版本 '3.1.11'
我的测试代码
def setup
@user = users(:marko)
end
test "successful edit" do
log_in_as(@user)
get edit_user_path(@user)
name = "Foo bar"
email = "foo@bar.com"
patch user_path(@user), params: { user: {
name: name,
email: email,
password: "password",
password_confirmation: "password" } }
assert_not flash.empty?
assert_redirected_to @user
follow_redirect!
@user.reload
assert_equal name, @user.name
assert_equal email, @user.email
end
有什么想法可能出了问题吗?
ActiveRecord::Fixture::FixtureError: table "users" has no column named "password"
的错误。 - degenPenguin