Search code examples
ruby-on-railsrubyimagepaperclip-validation

Paperclip synatx; which way is the proper way of handling validates_attachment_content_type


I'm using the ruby gem paperclip to handle image attachments and I'm adding validates_attachment_content_type to my books model. I just have a question on syntax.

Which is the proper way of doing the above?

validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"], message: "Only jpeg, png, and gif images types are allowed"

or

validates_attachment_content_type :image, content_type: /^image\/(png|gif|jpeg|jpg)/, message: "Only jpeg, png, and gif images types are allowed"

To me (total beginner) the first seems cleaner/clearer. Or does it not matter?


Solution

  • You can go with either one, I prefer the first one:

    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"], message: "Only jpeg, png, and gif images types are allowed"
    

    If you prefer to use a regex to validate, then use the second one.