【问题标题】:Paperclip synatx; which way is the proper way of handling validates_attachment_content_type回形针语法;哪种方式是处理 validates_attachment_content_type 的正确方式
【发布时间】:2015-02-20 21:58:21
【问题描述】:

我正在使用 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"

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

【问题讨论】:

    标签: ruby-on-rails ruby image paperclip-validation


    【解决方案1】:

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

    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"
    

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

    【讨论】:

    • 啊,谢谢你的回答。希望我的方向是正确的,@ 987654322@ 是content_type: xxxx 的rails 3 方式,对吗?
    • 不,不是真的,在 rails 3 上你会拥有 attr_accessible :image 而在 rails 4 上会变成 has_attached_file :image 所以只有安装图像属性更改的方法,而不是验证部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    相关资源
    最近更新 更多