Validate extension in Paperclip - Ruby on Rails

Geoff picture Geoff · Jul 2, 2011 · Viewed 10.9k times · Source

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?

Answer

abm picture abm · Jul 2, 2011

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

Edit:

Alternatively, to validate it with paperclip:

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

^ it will generate content-type mismatch errors automatically.