参数数量错误(给定 1,预期 0;必需关键字:io、文件名)- Active Storage

问题描述 投票:0回答:1

将 Rails 版本升级到 7.1.2 后,我遇到了主动存储问题。我正在尝试运行此代码:

banner = Banner.new
img_url = "https://t3.ftcdn.net/jpg/04/75/78/56/360_F_475785604_HDtTcxBFA0Av87F7JoFmpircCcatQ22b.jpg"
picture = URI.open(img_url, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE})
banner.img_v2.attach(io: picture, filename: File.basename(picture), key: "banners/v2/#{File.basename(picture)}.png")
banner.save

我在

banner.save
banner.img_v2.url
上收到以下错误:

/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/activestorage-7.1.2/app/models/active_storage/blob.rb:87:in `build_after_unfurling': wrong number of arguments (given 1, expected 0; required keywords: io, filename) (ArgumentError)

我发现this问题最接近我的问题,但没有帮助。感谢任何帮助或建议。

ruby-on-rails ruby rails-activestorage
1个回答
0
投票

我发现我正在使用与 Rails 6 兼容的主动存储初始化程序。我找到了源代码和解决方案here

错误来自这一行:

when Hash
  ActiveStorage::Blob.create_after_upload!({ metadata: { acl: acl } }.deep_merge(attachable))

更正:

when Hash
  ActiveStorage::Blob.build_after_unfurling \
    io: attachable[:io],
    filename: attachable[:filename],
    content_type: attachable[:content_type],
    metadata: { acl: acl }
© www.soinside.com 2019 - 2024. All rights reserved.