Post

Fixing Active Storage Photos Disappearing on Update

Fixing Active Storage Photos Disappearing on Update

The problem

When editing a record with a has_many_attached file field, Rails generates a hidden input for multiple file uploads by default. If no new files are selected, the form submits:

1
photos: [""]

Active Storage may interpret this as replacing the existing attachments with an empty collection, causing all photos to disappear.

The solution

Disable the hidden input by adding include_hidden: false to the file field:

1
2
3
4
<%= f.file_field :photos,
    multiple: true,
    include_hidden: false,
    accept: "image/*" %>

Now, when no files are selected, the photos parameter is omitted entirely, and existing attachments remain untouched.

Sources

It’s interesting to read the issues, PRs, and the whole flow of The comment: https://github.com/rails/rails/issues/48006#issuecomment-1620514629 PR that was rejected: https://github.com/rails/rails/pull/48658 PR that fixed the docs: https://github.com/rails/rails/pull/49350

This post is licensed under CC BY-NC 4.0 by the author.