本文介绍了从3.5升级到Paperclip 4.1时,没有validates_attachment_file_name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些看起来像纸夹回形针的代码:

We have code that looks like run of the mill paper clip:

has_merchants_attached_file :pdf,
    storage:          :s3,
    s3_credentials:   Mbc::DataStore.s3_credentials,
    s3_permissions:   :private,
    path:             ":identifier_template.pdf",
    bucket:           Mbc::DataStore.forms_and_templates_bucket_name


  validates_attachment_file_name :pdf, :matches => [/pdf\Z/]

哪个会产生错误:

undefined method `validates_attachment_file_name' for #<Class:0x007fba67d25fe0>

有趣的是,当我们将评分降至3.5,就会遇到相同的问题.

Interestingly enough, when we down grade back to 3.5, we encounter the same issue.

生成此消息的控制器是:

The controller that is generating this is:

def index
   @fidelity_templates = FidelityTemplate.order("identifier asc").all
end

另外:

def has_merchants_attached_file(attribute, options={})
  if Rails.env.test? || Rails.env.development?
     has_attached_file attribute,
     path: "paperclip_attachments/#{options[:path]}"
  else
    has_attached_file attribute, options
  end
end

有什么想法会导致这种情况吗?

Any thoughts on what could be causing this?

推荐答案

您可以在此处阅读有关提供的验证器的信息:

You can read about the provided validators here:

https://github.com/thoughtbot/paperclip#validations

包含的验证器为:

  • AttachmentContentTypeValidator
  • AttachmentPresenceValidator
  • AttachmentSizeValidator

它们可以通过以下两种方式之一使用:

They can be used in either of these ways:

# New style:
validates_with AttachmentPresenceValidator, :attributes => :avatar

# Old style:
validates_attachment_presence :avatar

更新...

如果您进一步阅读我上面给出的链接,您将进入有关安全性验证的部分(感谢Kirti Thorat):

If you read further down the link I've given above you'll get to a section on Security Validations (Thanks Kirti Thorat):

https://github.com/thoughtbot/paperclip#security-validations

他们提供了有关如何验证文件名格式的示例:

They give an example on how to validate the filename format:

# Validate filename
validates_attachment_file_name :avatar, :matches => [/png\Z/, /jpe?g\Z/]

从您的代码段来看,您的验证似乎应该保持原样.

From your code snippet it looks like your validation should work as-is.

但是,我从未见过使用这种语法的回形针:

However, I've never seen paperclip used with this syntax:

has_merchants_attached_file ...

也许这就是问题的根源?通常,您将使用以下内容将文件附加到模型:

Perhaps that's the source of your issues? You would usually use the following to attach files to your model:

has_attached_file :pdf ...

这篇关于从3.5升级到Paperclip 4.1时,没有validates_attachment_file_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:39