我正在阅读Michael Hartl在http://ruby.railstutorial.org/上的教程。
我在第六章,特别是代码清单 6.27,如下所示:
require 'spec_helper'
describe User do
before do
@user = User.new(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should be_valid }
end
现在用户对象看起来像这样:
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
before_save { |user| user.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, format: { with: VALID_EMAIL_REGEX }, uniquenes
{case_sensitive: false}
end
User 对象有六个属性:id、name、email、created_at、updated_at、password_digest。password_digest 是存储散列密码的地方。但正如您所见,密码和密码确认字段不在数据库中。只有 password_digest 是。作者声称我们不需要将它们存储在数据库中,而只是在内存中临时创建它们。但是当我从 rspec 测试运行代码时:
@user = User.new(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
我收到一条错误消息,告诉我字段密码和密码确认未定义。我该如何解决这个问题?
麦克风