我正在编写 Michael Hartl 的教程(第 6 章)。当我尝试在 rails 控制台中创建新用户时:
user = User.new(name: "Lord of Darkness", email: "LoD@hell.com")
我得到:
=> #<User id: nil, name: "Lord of Darkness", email: "LoD@hell.com",
created_at: nil, updated_at: nil, password_digest: nil>
这似乎是正确的。但是当我尝试保存时,我得到了这个:
irb(main):007:0> user.save
←[1m←[36m (0.0ms)←[0m ←[1mbegin transaction←[0m
←[1m←[35mUser Exists (0.0ms)←[0m SELECT 1 AS one FROM "users" WHERE LOWER("us
ers"."email") = LOWER('mhartl@example.com') LIMIT 1
←[1m←[36m (0.0ms)←[0m ←[1mrollback transaction←[0m
=> false
这绝对不是我想要的。我的 user_spec 正在通过所有测试。我的user.rb看起来像这样:
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
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 },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
end
是什么导致了这个保存问题?如果不解决此错误,我将无法继续本教程。