0

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?

Dipet
  • 323
  • 3
  • 14

1 Answers1

0

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.

neo
  • 4,078
  • 4
  • 25
  • 41
  • Ah, thanks for the answer. Hopefully I'm in the right direction with this, the `:content_type => xxx` is the rails 3 way of doing `content_type: xxxx` correct? – Dipet Feb 20 '15 at 22:10
  • No not really, on rails 3 you'd have `attr_accessible :image ` and on rails 4 that would become `has_attached_file :image` so only way you mount your image attribute changes, not the validation portion. – neo Feb 20 '15 at 22:15