tvalent2
tvalent2

Reputation: 5009

Rails validation error email is too short, email is invalid

In attempting to seed my database I ran into a validation error on my User model's email attribute. The error:

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

The thing is, my email is [email protected]. I have five characters. Sorry for the beginner question but I don't know what is going on. I recently followed Railscasts to reset a User's password, and enable CanCan. I'm not sure if CanCan would affect anything, but prior to exploring that new functionality I've been able to fully seed my database without problems. I've pasted in some of my code below. I'm running Rails 3.0.5 and Ruby 1.9.2.

An example of how I create a User in my seed file:

me = User.create(:email => '[email protected]', :password => 'test', :profile => my_profile)

User.rb model:

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?

Upvotes: 2

Views: 903

Answers (1)

James Chen
James Chen

Reputation: 10874

Add :email to attr_accessible to allow mass assignment on it. Without that the email field will not even be set so validation will fail.

Upvotes: 3

Related Questions