Validate extension in Paperclip - Ruby on Rails

前端 未结 1 1173
迷失自我
迷失自我 2021-01-04 21:59

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 e

1条回答
  •  有刺的猬
    2021-01-04 22:20

    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.

    0 讨论(0)
提交回复
热议问题