Retain upload after form redisplay with Active Storage

When uploading files with Active Storage, after the form was submitted and the form gets redisplayed, for example when the validation for the model that form is based on fails for some reason, the file is gone.

As far is I know, currently there is no easy way to cache or retain it between form redisplays. Has anyone here implemented something like that on their project with Active Storage yet?

Shrine has a great Plugin for that purpose:

The cached_attachment_data plugin adds the ability to retain the cached file across form redisplays, which means the file doesn’t have to be reuploaded in case of validation errors.

Basically one just has to add a hidden field to a form, which stores the upload on form redisplays:

form.hidden_field :image, value: @model.cached_image_data

``

Implementing something similar would be a worthwhile feature to try and contribute back to Rails or is there anything that prevents this?

1 Like

Same topic, and same without answers yet :frowning: