【问题标题】:IOError (closed stream) on ActiveStorage::Attached::One#attach (Rails 6)ActiveStorage::Attached::One#attach(Rails 6)上的 IOError(关闭流)
【发布时间】:2020-01-17 15:43:32
【问题描述】:

我正在尝试使用新的 Rails 6 syntax for downloading files 复制附件文件

if original.attachment.attached?
  original.attachment.open do |tempfile|
    clone.attachment.attach({
      io: tempfile,
      filename: original.attachment.blob.filename,
      content_type: original.attachment.blob.content_type
    })
  end
end

我收到此错误:IOError (closed stream)

如果我在执行期间检查 /tmp/ 文件夹,则临时文件副本就在那里。我不知道为什么会出现这个错误。

我在 macOS 10.13 High Sierra 上运行 Rails 6.0.0 和 ruby​​ 2.6.5p114

【问题讨论】:

    标签: ruby-on-rails rails-activestorage ruby-on-rails-6


    【解决方案1】:

    好的,我找到了答案。在 rails 6 中,您必须在附加时再次打开临时文件,如下所示:

    io: File.open(tempfile.path),

    希望这对遇到同样问题的人有所帮助。

    【讨论】:

    • 谢谢!真的很困惑为什么升级到 Rails 6 后这不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 2021-01-27
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多