Search code examples
ruby-on-railsrails-activestorageruby-on-rails-6

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


I'm trying to duplicate an attached file, using the new 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

And I get this error: IOError (closed stream)

If I check the /tmp/ folder during execution the tempfile copy is there. I don't know why is this error being raised.

I'm running Rails 6.0.0 and ruby 2.6.5p114 on macOS 10.13 High Sierra


Solution

  • Okay, I found the answer. In rails 6 you have to open the tempfile again, when attaching, like this:

    io: File.open(tempfile.path),

    Hope this helps anyone with the same issue.