验证Paperclip中的扩展-Ruby on Rails [英] Validate extension in Paperclip - Ruby on Rails

查看:81
本文介绍了验证Paperclip中的扩展-Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现Paperclip可以验证文件内容类型,即image/jpeg,但是我想专门验证扩展名.这是因为我正在使用一个晦涩的扩展名,该扩展名不会获得一致的内容类型.任何人都知道这是否可行,或者是执行此操作的好方法吗?

I've found that Paperclip can validate file content type, i.e. image/jpeg, but I want to specifically validate the extension. This is because I'm working with an obscure extension that won't get a consistent content type. Anyone know if this is doable, or a good way to do this?

推荐答案

猜猜,没有必要使用回形针的方法对其进行验证.您宁可使用类似以下内容的方法:

Guess, there is no need to validate it with paperclip's method. You can rather use something like:

has_attached_file :attachment
validates_format_of :attachment_file_name, :with => %r{\.(docx|doc|pdf)$}i

或者,使用回形针对其进行验证:

Alternatively, to validate it with paperclip:

validates_attachment_content_type :attachment, :content_type => 'text/plain'

^它会自动生成内容类型不匹配错误.

^ it will generate content-type mismatch errors automatically.

这篇关于验证Paperclip中的扩展-Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆