Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I restrict Paperclip to only accept images?

How can I restrict Paperclip to only accept images? I'm using Amazon S3 for storage if that's relevant. Thanks for reading.

like image 406
ben Avatar asked Jan 13 '11 04:01

ben


2 Answers

From https://makandracards.com/makandra/606-only-allow-pictures-as-paperclip-attachments

validates_attachment_content_type :image, :content_type => /^image\/(jpg|jpeg|pjpeg|png|x-png|gif)$/, :message => 'file type is not allowed (only jpeg/png/gif images)'
like image 57
moonpatrol Avatar answered Nov 06 '22 20:11

moonpatrol


Paperclip has validation methods like validates_attachment_presence, validates_attachment_content_type, and validates_attachment_size.

So all you need to do is pass mime types of images you'd like to have as attachments:

validates_attachment_content_type 'image/png', 'image/jpg'
like image 36
Eimantas Avatar answered Nov 06 '22 21:11

Eimantas