从3.5升级到Paperclip 4.1时没有validates_attachment_file_name

我们的代码看起来像磨纸夹的运行:

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 # 

有趣的是,当我们降级到3.5时,我们遇到了同样的问题。

生成它的控制器是:

 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 

有什么可能导致这种情况的想法?

您可以在此处阅读所提供的validation器:

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

包含的validation人是:

  • AttachmentContentTypeValidator
  • AttachmentPresenceValidator
  • AttachmentSizeValidator

它们可以以下列方式之一使用:

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

更新……

如果你进一步阅读我上面给出的链接,你将进入安全validation部分(感谢Kirti Thorat):

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

他们举例说明如何validation文件名格式:

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

从您的代码片段看,您的validation应该按原样运行。

但是,我从未见过使用此语法的paperclip:

 has_merchants_attached_file ... 

也许这是你问题的根源? 您通常会使用以下方法将文件附加到您的模型:

 has_attached_file :pdf ...