3

Here's how I am using paperclip in my model:

has_attached_file :photo,
  styles: {
    display: {
      geometry: "146x153#",
      format: :jpg,
    },
    message: {
      geometry: "48x48#",
      format: :jpg,
    }
  }

validates_attachment_content_type :photo, content_type: ['image/jpeg', 'image/png','image/gif']
validates_attachment_size         :photo, less_than:    2.megabytes, unless: :record_is_new?

It works fine, however, I want to make the image upload optional i.e. if the user does not wish to upload a picture, the validation should not apply.

oldhomemovie
  • 14,621
  • 13
  • 64
  • 99
geeku
  • 65
  • 9

2 Answers2

2

Solved this, the model had this validation: validates_attachment_presence: photo that i completely overlooked.

geeku
  • 65
  • 9
0

Try validation conditionally, adding this to validations:

validates_something_on :photo, ..., unless: Proc.new { |record| record[:image].nil? }

Result:

validates_attachment_content_type :photo, content_type: ['image/jpeg', 'image/png','image/gif'], unless: Proc.new { |record| record[:image].nil? }
validates_attachment_size         :photo, less_than:    2.megabytes,                             unless: Proc.new { |record| record[:image].nil? }
oldhomemovie
  • 14,621
  • 13
  • 64
  • 99