Paperclip允许用户在Rails 4

时间:2015-05-28 16:18:15

标签: ruby-on-rails validation ruby-on-rails-4 model-view-controller model

我在模型中创建了一个名为 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及以上的图片?

1 个答案:

答案 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