2012-02-01 128 views
2

在试图为我的数据库播种时,我遇到了我的用户模型的电子邮件属性上的验证错误。错误:Rails验证错误电子邮件太短,电子邮件无效

Validation failed: Email is too short (minimum is 5 characters), Email is invalid 

事情是,我的电子邮件是[email protected]。我有五个角色。对于初学者的问题抱歉,但我不知道发生了什么。我最近跟踪Railscasts重置用户密码,并启用CanCan。我不确定CanCan是否会影响任何内容,但是在探索新功能之前,我已经能够完整地为我的数据库提供种子,而且没有任何问题。我在下面粘贴了一些我的代码。我正在运行Rails 3.0.5Ruby 1.9.2

我如何在我的种子文件中创建用户的示例:

me = User.create(:email => '[email protected]', :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.-][email protected][\w.-]+[.][a-z]{2,4}$/i } 
    validates :password, :confirmation => true, 
        :length => { :within => 4..20 }, 
        :presence => true, 
        :if => :password_required? 

回答

3

地址:电子邮件attr_accessible允许在质量分配它。没有这个电子邮件字段将不会被设置,因此验证将失败。