0

I have a regular users model with address columns. But when a person attempts to register with email and password he receives the error: zip address and town cannot be blank. All you should need to register is email and password. How do I fix user creation ?

User Model:

class User < ActiveRecord::Base
      attr_accessible :zip, :state, :town, :address, :email, :password, :password_confirmation,

      attr_accessor :password
      validates_confirmation_of :password
      validates :email, presence: true, format: { with: VALID_E_REGEX }, uniqueness: { case_sensitive: false }
      validates :password, presence: true, format:{  with: VALID_P_REGEX }, if: proc{ password_salt.blank? || password_hash.blank? } 
      validates :zip, format: { with: VALID_ZIP_REGEX }
      validates :address, length: { minimum: 5 }
      validates :town, length: { minimum: 4 }
    end

User controller:

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      redirect_to root_url, :notice => "Signed up!"
    else
      render "new"
    end
  end 
end

Error:

!! #<ActiveRecord::RecordInvalid: Validation failed: Phone number is invalid, Zip is invalid, Address is too short (minimum is 5 characters), Town is too short (minimum is 4 characters)>
1

3 Answers 3

2

It depends on what you want the actual behavior to be.

  • If you don't want to validate the extra attributes, just remove their validations as Vamsi says.

  • If you want to validate the extra attributes, but not on creation of the user object, you can add on: :update, like so:

    validates :zip, format: { with: VALID_ZIP_REGEX }, on: :update

  • If you want want to validate the extra attributes, but only when they've actually been entered, you can add allow_blank: true like so (or allow_nil, again, depending on what your needs are):

    validates :zip, format: { with: VALID_ZIP_REGEX }, allow_blank: true

For more information about validations and their options, check out the Active Record Validations guide.

Sign up to request clarification or add additional context in comments.

Comments

1

How about making the validation conditional.

validates :address, length: { minimum: 5 }, if: :address

Comments

0

Remove the unnecessary validates in the model. As simple as that...

If you want to use it only if value exists you can use the allow_nil or allow_blank option of validate. or you can also use the if conditional.

class User < ActiveRecord::Base
      attr_accessible :zip, :state, :town, :address, :email, :password, :password_confirmation,

      attr_accessor :password
      validates_confirmation_of :password
      validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
      validates :password, presence: true, format:{  with: VALID_PASSWORD_REGEX }, if: proc{ password_salt.blank? || password_hash.blank? }
      validates :zip, format: { with: VALID_ZIP_REGEX }, allow_blank: true
      validates :address, length: { minimum: 5 }, allow_blank: true
      validates :town, length: { minimum: 4 }, allow_blank: true
    end

3 Comments

but wait when i add address information i want it to be validated.
Yes but there is another page on the app where they may add address info to the user model. I want to make sure those are correct when they come in.
Then you can go for the condition based validation. validates :address, length: { minimum: 5 }, if: :address

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.