0

我正在使用 ruby​​ gem 回形针来处理图像附件,并且正在添加validates_attachment_content_type到我的书籍模型中。我只是对语法有疑问。

执行上述操作的正确方法是什么?

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"

或者

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

对我(完全是初学者)来说,第一个似乎更干净/更清晰。还是没关系?

4

1 回答 1

0

你可以选择任何一个,我更喜欢第一个:

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"

如果您更喜欢使用正则表达式进行验证,请使用第二个。

于 2015-02-20T22:04:11.677 回答