How can I restrict Paperclip to only accept images?

烈酒焚心 提交于 2019-12-20 18:31:06

问题


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


回答1:


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'



回答2:


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


来源:https://stackoverflow.com/questions/4676902/how-can-i-restrict-paperclip-to-only-accept-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!