`check_validity!' 出错

Error with `check_validity!'

我在尝试迁移时遇到此错误。我查看了我的 user.rb,但我不明白 validate_format_of 是如何导致错误的。下面是我的 user.rb 和错误日志:

ArgumentError: Either :with or :without must be supplied (but not both)
/Users/admin/.rvm/gems/ruby-2.2.0/gems/activemodel-4.1.6/lib/active_model/validations/format.rb:17:in `check_validity!'
/Users/admin/.rvm/gems/ruby-2.2.0/gems/activemodel-4.1.6/lib/active_model/validator.rb:157:in `initialize'
/Users/admin/.rvm/gems/ruby-2.2.0/gems/activemodel-4.1.6/lib/active_model/validations/with.rb:89:in `new'
/Users/admin/.rvm/gems/ruby-2.2.0/gems/activemodel-4.1.6/lib/active_model/validations/with.rb:89:in `block in validates_with'
/Users/admin/.rvm/gems/ruby-2.2.0/gems/activemodel-4.1.6/lib/active_model/validations/with.rb:88:in `each'
/Users/admin/.rvm/gems/ruby-2.2.0/gems/activemodel-4.1.6/lib/active_model/validations/with.rb:88:in `validates_with'
/Users/admin/.rvm/gems/ruby-2.2.0/gems/activemodel-4.1.6/lib/active_model/validations/format.rb:109:in `validates_format_of'
/Users/admin/Documents/workspace/ruby_on_rails/zoan/app/models/user.rb:19:in `<class:User>'
/Users/admin/Documents/workspace/ruby_on_rails/zoan/app/models/user.rb:1:in `<top (required)>'

型号:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable

  TEMP_EMAIL_PREFIX = 'main@gmail'
  TEMP_EMAIL_REGEX = /\Amain@gmail/

  attr_accessor :login

  self.per_page = 20

  extend FriendlyId
  friendly_id :username, use: [:slugged, :finders]

  devise :database_authenticatable, :registerable, :confirmable,
    :recoverable, :rememberable, :trackable, :validatable, :omniauthable
  validates :name, presence: true, length: { maximum: 100 }
  validates :username, presence: true, length: { maximum: 20 }, :uniqueness => { case_sensitive: false }
  validates_format_of :email, on: :update

  has_many :tweets
  has_many :relationships
  has_many :friends, through: :relationships
  has_many :inverse_relationships, class_name: "Relationship", foreign_key: "friend_id"
  has_many :inverse_friends, through: :inverse_relationships, source: :user
  has_many :favorites
  has_many :votes
  has_many :retweets, foreign_key: "retweeter_id"

  mount_uploader :avatar, AvatarUploader
  mount_uploader :cover, CoverUploader

  # def self.find_for_database_authentication(warden_conditions)
  #   conditions = warden_conditions.dup
  #   if login = conditions.delete(:login)
  #     where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
  #   else
  #     where(conditions).first
  #   end
  # end

  def self.find_for_oauth(auth, signed_in_resource = nil)

    # Get the identity and user if they exist
    identity = Identity.find_for_oauth(auth)

    # If a signed_in_resource is provided it always overrides the existing user
    # to prevent the identity being locked with accidentally created accounts.
    # Note that this may leave zombie accounts (with no associated identity) which
    # can be cleaned up at a later date.
    user = signed_in_resource ? signed_in_resource : identity.user

    # Create the user if needed
    if user.nil?

      # Get the existing user by email if the provider gives us a verified email.
      # If no verified email was provided we assign a temporary email and ask the
      # user to verify it on the next step via UsersController.finish_signup
      email_is_verified = auth.info.email && (auth.info.verified || auth.info.verified_email)
      email = auth.info.email if email_is_verified
      user = User.where(:email => email).first if email

      # Create the user if it's a new registration
      if user.nil?
        user = User.new(
          name: auth.extra.raw_info.name,
          #username: auth.info.nickname || auth.uid,
          email: email ? email : "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
          password: Devise.friendly_token[0,20]
        )
        user.skip_confirmation!
        user.save!
      end
    end

    # Associate the identity with the user if needed
    if identity.user != user
      identity.user = user
      identity.save!
    end

    user
  end

  def email_verified?
    self.email && self.email !~ TEMP_EMAIL_REGEX
  end

end

validates_format_of :email, on: :update

您必须指定如何查看电子邮件。使用 "with"

查看文档中的示例:http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_format_of

基本上,您需要为 validates_format_of 提供一个正则表达式,以便它可以与您的字符串进行比较。

您可以使用 :with 选项提供正则表达式。

在您的示例中,要在更新时验证电子邮件,请执行以下操作:

validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :update

正则表达式适用于大多数电子邮件格式:http://rubular.com/r/YEPtKO3j5L