In Rails 6 I have a form with a file field and I am using activestorage to store the file. If validations fail after submitting the form, the form is redisplayed showing the validation errors. How can I retain the file added to the file field when redisplaying the form so that the user does not have to add the file again to the form?
There is already a similar question for rails 5: Active Storage: Best practice to retain/cache uploaded file when form redisplays, however the solution there only applies to Rails 5.
Active Storage analyzes files once they've been uploaded by queuing a job in Active Job. Analyzed files will store additional information in the metadata hash, including analyzed: true . You can check whether a blob has been analyzed by calling analyzed? on it.
Since Rails 6 does not store the file on assignment, the workaround I found was to enable direct upload on the file field. This will upload the file via javascript before the form submission.
= f.file_field :doc, direct_upload: true
For this to work, you also need to add activestorage.js to your bundle as described in the Active Storage guide.
After that change one can use the method described in the question Active Storage: Best practice to retain/cache uploaded file when form redisplays. That means adding the signed_id to your form in a hidden field as follows:
= f.file_field :doc, direct_upload: true
= f.hidden_field :doc, value: f.object.doc.signed_id if f.object.doc.attached?
In addition to Robban answer, for multiple attachments, and doing some previewing
= f.file_field :attachments, multiple: true, direct_upload: true
- f.object.attachments.each do |attach|
= f.hidden_field :attachments, value: attach.signed_id, multiple: true
= image_tag attach.preview(resize_to_limit: [100, 100]) if attach.previewable?
= image_tag attach.variant(resize_to_limit: [100, 100]) if attach.variable?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With