Ruby on rails Rails:用户创建其他验证

Ruby on rails Rails:用户创建其他验证,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个带有地址列的常规用户模型。但当一个人试图用电子邮件和密码注册时,他会收到一个错误:zip地址和城镇不能为空。您只需注册电子邮件和密码。如何修复用户创建 用户模型: class User < ActiveRecord::Base attr_accessible :zip, :state, :town, :address, :email, :password, :password_confirmation, attr_accessor :password

我有一个带有地址列的常规用户模型。但当一个人试图用电子邮件和密码注册时,他会收到一个错误:zip地址和城镇不能为空。您只需注册电子邮件和密码。如何修复用户创建

用户模型:

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
class用户
用户控制器:

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
class UsersController“已注册!”
其他的
呈现“新”
结束
结束
结束
错误:

!! #<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)>
#

删除模型中不必要的验证。就这么简单

如果只想在值存在时使用它,可以使用validate的allow\u nil或allow\u blank选项。或者也可以使用if条件

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
validates :address, length: { minimum: 5 }, if: :address
class用户
删除模型中不必要的验证。就这么简单

如果只想在值存在时使用它,可以使用validate的allow\u nil或allow\u blank选项。或者也可以使用if条件

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
validates :address, length: { minimum: 5 }, if: :address
class用户
删除模型中不必要的验证。就这么简单

如果只想在值存在时使用它,可以使用validate的allow\u nil或allow\u blank选项。或者也可以使用if条件

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
validates :address, length: { minimum: 5 }, if: :address
class用户
删除模型中不必要的验证。就这么简单

如果只想在值存在时使用它,可以使用validate的allow\u nil或allow\u blank选项。或者也可以使用if条件

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
validates :address, length: { minimum: 5 }, if: :address
class用户
让验证有条件怎么样

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
validates :address, length: { minimum: 5 }, if: :address

让验证有条件怎么样

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
validates :address, length: { minimum: 5 }, if: :address

让验证有条件怎么样

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
validates :address, length: { minimum: 5 }, if: :address

让验证有条件怎么样

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
validates :address, length: { minimum: 5 }, if: :address

这取决于你想要的实际行为

  • 如果您不想验证额外的属性,只需按照Vamsi的说法删除它们的验证

  • 如果要验证额外属性,但不是在创建用户对象时,可以在::update上添加
    ,如下所示:

    验证:zip,格式:{with:VALID\u zip\u REGEX},on::update

  • 如果您希望验证额外属性,但仅在实际输入这些属性时,您可以像这样添加
    allow\u blank:true
    (或者
    allow\u nil
    ,这同样取决于您的需要):

    验证:zip,格式:{with:VALID\u zip\u REGEX},允许\u blank:true


有关验证及其选项的更多信息,请查看。

这取决于您希望实际行为是什么