Rails - Paperclip validating attachment size when it shouldn't be?

旧街凉风 提交于 2019-12-03 02:35:49

validates_attachment_size :image, :in => 0.megabytes..2.megabytes

works now

Sytse
validates_attachment_size :image, :less_than => 25.megabytes, 
                          :unless => Proc.new {|m| m[:image].nil?}

works perfectly for me

Paperclip's validation only checks the range, and doesn't care about the :allow_nil => true

What you can do is try to set :min => nil or :min => -1, maybe that will work.

Update: This will not work in the latest version of Paperclip since they have changed how validations work. What you could try instead is:

validates_attachment_size :image, :less_than => 2.megabytes, 
   :unless => Proc.new {|model| model.image }

Try the following code.

validate :image_present

def image_present
  if image.present? && image_file_size < 2.megabytes
    errors.add(:file_size, "file size must be between 0 and 2 megabytes.")
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!