如何限制Paperclip仅接受图像?

时间:2022-11-25 00:24:04

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

如何限制Paperclip仅接受图像?如果相关,我正在使用Amazon S3进行存储。谢谢阅读。

2 个解决方案

#1


13  

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

Paperclip具有验证方法,如validates_attachment_presence,validates_attachment_content_type和validates_attachment_size。

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

所以你需要做的就是传递你想要作为附件的mime类型的图像:

validates_attachment_content_type 'image/png', 'image/jpg'

#2


24  

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

来自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)'

#1


13  

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

Paperclip具有验证方法,如validates_attachment_presence,validates_attachment_content_type和validates_attachment_size。

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

所以你需要做的就是传递你想要作为附件的mime类型的图像:

validates_attachment_content_type 'image/png', 'image/jpg'

#2


24  

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

来自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)'