Reputation: 1221
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.
Upvotes: 11
Views: 4111
Reputation: 3077
I found the solution to store file with form post (without direct upload). But it requires additional code to the usual scaffolded code @mention = Mention.create(mention_params)
controller code:
@mention.screenshot.blob.upload(mention_params[:screenshot])
@mention.screenshot.blob.save
while debugging check that it creates a record in active_storage_blob table.
Once file uploaded you can reuse signed_id on next submissions (when model validation does not pass).
view code:
f.hidden_field :screenshot, value: ...screenshot.signed_id if ...screenshot.attached? %>
It is not a clean solution but I think you'll get the point.
Upvotes: 0
Reputation: 791
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?
Upvotes: 2
Reputation: 1221
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?
Upvotes: 7