2012-02-01 129 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允許在質量分配它。沒有這個電子郵件字段將不會被設置,因此驗證將失敗。