ActiveStorage::Attached::One#attach (Rails 6) 上的 IOError(关闭流)

IOError (closed stream) on ActiveStorage::Attached::One#attach (Rails 6)

我正在尝试使用新的 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

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

io: File.open(tempfile.path),

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