Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using activestorage in Rails 6, how do I retain a file when redisplaying a form?

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.

like image 940
Robban Avatar asked Jun 18 '19 12:06

Robban


People also ask

How does active storage work?

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.


2 Answers

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?
like image 147
Robban Avatar answered Oct 31 '22 14:10

Robban


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?
like image 30
Ccastillop Avatar answered Oct 31 '22 15:10

Ccastillop