Repository navigation
Consolidate ActiveStorage examples in README - #164
Conversation
|
Hi, thanks! Based on your change, it looks like the implementations for rails 5 or 6 are actually the same? I don't use AS myself, so this is purely based on input given to me by other developers :) If they are the same, I think we could jsut keep the Rails 5 examples for has_one/has_many and remove the rails version comments.. |
|
Yes, they're the same. The old Rails 6 snippet used avatar.open plus File.open(tempfile.path). That tempfile is deleted when the block ends, and the upload happens later, when the unsaved copy is saved, so the file is already gone. That's the Errno::ENOENT in #148. download and attach(io:, filename:, content_type:) are the same in Rails 5.2 and 6.1. The hash-rocket versus keyword syntax is just style. The Rails 6 block only repeated has_one_attached; the has_many_attached examples were already using StringIO. I've pushed that: removed the Rails 6 block and dropped the Rails 5.2 prefixes. Carrierwave, Shrine, and the shallow-copy section are unchanged. Title and description updated. |
|
Thanks! |
Problem
The ActiveStorage example in the README labelled # Rails 6 raises an error on Rails 6.1 and later:
Cause
original.avatar.openyields a tempfile and deletes it as soon as the block exits. Inside the block,kopy.avatar.attach(...)doesn't upload anything yet —kopyis unpersisted, so Active Storage defers the upload until the record is saved. That save happens after the block has closed and the tempfile has already been removed, so Rails looks for a file that no longer exists.Fix
The other examples already use
StringIO.new(download), which works on every Active Storage version. So the Rails 6 block wasn't just broken, it wasn't needed at all. I removed it.I also dropped the
Rails 5.2prefixes. These examples work on any version, and the labels made it look like readers have to pick one based on their Rails version.Fixes #148
Note
This is a documentation-only change; no library code is affected.