在尝试为我的数据库设定种子时,我在用户模型的电子邮件属性上遇到了验证错误。错误:
Validation failed: Email is too short (minimum is 5 characters), Email is invalid
问题是,我的电子邮件是xxxxxxxx@gmail.com
。我有五个字符。对不起初学者的问题,但我不知道发生了什么。我最近关注Railscasts重置了用户密码,并启用了CanCan。我不确定CanCan是否会影响任何事情,但在探索新功能之前,我已经能够完全播种我的数据库而没有任何问题。我已粘贴在下面的一些代码中。我正在运行Rails 3.0.5
和Ruby 1.9.2
。
我在种子文件中创建用户的示例:
me = User.create(:email => 'me@gmail.com', :password => 'test', :profile => my_profile)
User.rb型号:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :password, :password_confirmation
before_save :encrypt_new_password
before_create { generate_token(:auth_token) }
before_validation :downcase_email
has_one :profile, :dependent => :destroy
accepts_nested_attributes_for :profile
validates :email, :uniqueness => true,
:length => { :within => 5..50 },
:format => { :with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i }
validates :password, :confirmation => true,
:length => { :within => 4..20 },
:presence => true,
:if => :password_required?
答案 0 :(得分:3)
添加:发送电子邮件至attr_accessible以允许批量分配。没有它,电子邮件字段甚至不会被设置,因此验证将失败。