no validates_attachment_file_name when upgrading to Paperclip 4.1 from 3.5

女生的网名这么多〃 提交于 2019-12-22 10:14:27

问题


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/]

Which generates an error:

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

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

Additionally:

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?


回答1:


You can read about the provided validators here:

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

The included validators are:

  • 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

UPDATE ...

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 ...


来源:https://stackoverflow.com/questions/21910714/no-validates-attachment-file-name-when-upgrading-to-paperclip-4-1-from-3-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!