El correo electrónico de error de validación de rieles es demasiado corto, el correo electrónico no es válido

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 xxxxxxxx@gmail.com. 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 y Ruby 1.9.2.

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

me = User.create(:email => 'me@gmail.com', :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?

preguntado el 01 de febrero de 12 a las 03:02

1 Respuestas

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.

Respondido 01 Feb 12, 09:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.