问题
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.
回答1:
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?
回答2:
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?
来源:https://stackoverflow.com/questions/56649565/when-using-activestorage-in-rails-6-how-do-i-retain-a-file-when-redisplaying-a