Validate multiple content-types with Paperclip and Rails
N

1

9

I'm using the Paperclip gem for Rails in order to allow users to upload a photo of themselves. I obviously only want to accept jpeg, gif or png images. What is the proper way to validate these files were uploaded instead of something else like a word document?

According to the Paperclip docs, I validate content-type with:

validates_attachment :document, content_type: "application/pdf"

What is the proper way to validate the above different image formats (gif, png, jpeg)?

Noncommittal answered 26/7, 2013 at 9:54 Comment(0)
C
11
class Doc
  has_attached_file :document
  validates_attachment_content_type :document, 
                                    :content_type => /^document\/(png|gif|jpeg)/,
                                    :message => 'only (png/gif/jpeg) images'
end
Contraption answered 26/7, 2013 at 10:1 Comment(1)
there is a typo. validates_attachment_content_type :image, should be validates_attachment_content_type :documentNadeau

© 2022 - 2024 — McMap. All rights reserved.