ruby-on-railsrails-activestorageruby-on-rails-5.2

Active Storage: Best practice to retain/cache uploaded file when form redisplays


When uploading files with Active Storage, when a file is finished uploading and the form gets redisplayed, for example when the validation for that form fails for some reason, the file is gone.

Is there a way to cache or retain it between form redisplays? Shrine has a nice Plugin for that purpose, I'm looking for something similar for Active Storage.


Solution

  • Here's a solution to make ActiveStorage files persist on form redisplay: f.hidden_field :image, value: f.object.image.signed_id if f.object.image.attached? f.file_field :image