如何结合载波有条件地显示存在验证?

How to display a presence validation conditionally in combination with carrierwave?

我有一个经过以下验证的图像模型

validates :image, presence: true, image_size: { width: { min: 400 },
height: { min: 400 } }, :file_size => { :maximum => 5.megabytes.to_i }

现在我也有一个载波验证:

def extension_whitelist
  %w(jpg jpeg gif png)
end

当我提交 .txt 文件时,我得到以下信息:

The form contains 2 errors. Image can't be blank Image You are not allowed to upload "txt" files, allowed types: jpg, jpeg, gif, png

第一条错误信息不应该出现。因为我实际上是在提交一些东西。

如何删除第一个错误消息? Image can't be blank

如果没有图像完整性错误,您可以跳过存在验证:

class User < ApplicationRecord
  mount_uploader :image, ImageUploader

  validates :image,
            presence: true,
            if: ->(record) { record.image_integrity_error.blank? }
end

user = User.new(image: File.open('tmp/.keep'))
user.valid? #=> false
user.errors.full_messages
#=> ["Image You are not allowed to upload \"\" files, allowed types: jpg, jpeg, gif, png"]