我在模型中创建了一个名为 dimensions_validator.rb 的文件权限
class DimensionsValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if record.send("#{attribute}?".to_sym)
dimensions = Paperclip::Geometry.from_file(value.queued_for_write[:original].path)
width = options[:width]
height = options[:height]
record.errors[attribute] << "Width must be at least #{width}px" unless dimensions.width = width
record.errors[attribute] << "Height must be at least #{height}px" unless dimensions.height = height
end
end
end
在产品模型中我做
validates :image, dimensions: { width: 800, height: 500 }
问题:如何让用户上传800x500及以上的图片?
答案 0 :(得分:1)
只需替换以下行中的条件和消息:
record.errors[attribute] << "Width must be at least #{width}px" if dimensions.width < width
record.errors[attribute] << "Height must be at least #{height}px" if dimensions.height < height