Ruby on rails 3 Rails验证错误电子邮件太短,电子邮件无效
在尝试为数据库设定种子时,我在用户模型的电子邮件属性上遇到了验证错误。错误:Ruby on rails 3 Rails验证错误电子邮件太短,电子邮件无效,ruby-on-rails-3,validation,Ruby On Rails 3,Validation,在尝试为数据库设定种子时,我在用户模型的电子邮件属性上遇到了验证错误。错误: Validation failed: Email is too short (minimum is 5 characters), Email is invalid 问题是,我的电子邮件是xxxxxxxx@gmail.com。我有五个字。对不起,我问了一个初学者的问题,但我不知道是怎么回事。我最近跟随Railscasts重置了用户密码,并启用了CanCan。我不确定CanCan是否会影响任何东西,但在探索这一新功能之前
Validation failed: Email is too short (minimum is 5 characters), Email is invalid
问题是,我的电子邮件是xxxxxxxx@gmail.com
。我有五个字。对不起,我问了一个初学者的问题,但我不知道是怎么回事。我最近跟随Railscasts重置了用户密码,并启用了CanCan。我不确定CanCan是否会影响任何东西,但在探索这一新功能之前,我已经能够毫无问题地为我的数据库进行全面的种子植入。我已经在下面粘贴了一些代码。我正在运行rails3.0.5
和Ruby 1.9.2
我如何在种子文件中创建用户的示例:
me = User.create(:email => 'me@gmail.com', :password => 'test', :profile => my_profile)
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?
User.rb型号:
me = User.create(:email => 'me@gmail.com', :password => 'test', :profile => my_profile)
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?
class用户:destroy
接受\u嵌套的\u属性\u:profile
验证:电子邮件,:唯一性=>true,
:length=>{:within=>5..50},
:format=>{:with=>/^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i}
验证:密码,:确认=>true,
:length=>{:within=>4..20},
:presence=>true,
:if=>:需要密码吗?
添加:可通过电子邮件发送至attr_,以允许对其进行批量分配。否则,电子邮件字段甚至不会设置,因此验证将失败