Skip to content

Consolidate ActiveStorage examples in README - #164

Merged
moiristo merged 2 commits into
moiristo:masterfrom
AmeenKhan12345:fix-activestorage-readme-example
Oct 9, 2026
Merged

moiristo merged 2 commits into
moiristo:masterfrom
AmeenKhan12345:fix-activestorage-readme-example

Conversation

@AmeenKhan12345

@AmeenKhan12345 AmeenKhan12345 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The ActiveStorage example in the README labelled # Rails 6 raises an error on Rails 6.1 and later:

Errno::ENOENT (No such file or directory @ rb_file_s_size - /tmp/ActiveStorage...)

Cause

original.avatar.open yields a tempfile and deletes it as soon as the block exits. Inside the block, kopy.avatar.attach(...) doesn't upload anything yet — kopy is 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.2 prefixes. 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.

@moiristo

moiristo commented Oct 9, 2026

Copy link
Copy Markdown
Owner

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..

@AmeenKhan12345 AmeenKhan12345 changed the title Fix ActiveStorage example for Rails 6.1+ in README Consolidate ActiveStorage examples in README Oct 9, 2026
@AmeenKhan12345

Copy link
Copy Markdown
Contributor Author

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.

@moiristo
moiristo merged commit f169000 into moiristo:master Oct 9, 2026
@moiristo

moiristo commented Oct 9, 2026

Copy link
Copy Markdown
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix documentation for Active Storage usage in Rails 6.1

2 participants