gpt4 book ai didi

ruby-on-rails - 从 3.5 升级到 Paperclip 4.1 时没有 validates_attachment_file_name

转载 作者:数据小太阳 更新时间:2023-10-29 08:04:22 26 4
gpt4 key购买 nike

我们的代码看起来像普通回形针:

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 时,我们遇到了同样的问题。

生成这个的 Controller 是:

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

对可能导致此问题的原因有任何想法吗?

最佳答案

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

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

包含的验证器是:

  • 附件内容类型验证器
  • 附件存在验证器
  • 附件大小验证器

它们可以通过以下任一方式使用:

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

# Old style:
validates_attachment_presence :avatar

更新...

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

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

他们给出了一个关于如何验证文件名格式的例子:

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

从您的代码片段来看,您的验证应该按原样工作。

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

has_merchants_attached_file ...

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

has_attached_file :pdf ...

关于ruby-on-rails - 从 3.5 升级到 Paperclip 4.1 时没有 validates_attachment_file_name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21910714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com