What I'd like to know is very simple:
How to retain uploaded files on form resubmission in Rails6 with ActiveStorage?
I have checked the below as a similar question.
When using activestorage in Rails 6, how do I retain a file when redisplaying a form?
The summary of suggested solution in it is like this:
Active Storage store attachments after the record is saved rather than immediately. So, if you want to persist assigned file after validation error, you must upload and save the file.
For example,
def update
...
# `obj` is your model that using `has_one_attached`.
if(obj.update)
redirect_to ...
else
obj.attachment_changes.each do |_, change|
if change.is_a?(ActiveStorage::Attached::Changes::CreateOne)
change.upload
change.blob.save
end
end
...
end
end
or, use direct_upload:
= f.file_field :doc, direct_upload: true
= f.hidden_field :doc, value: f.object.doc.signed_id if f.object.doc.attached?
By using these solutions, yeah, I managed to retain a file when redisplaying a form. However, these are against this pr intention. pr says
It’s of little use to identify an invalid file after it’s already been shipped off to storage:
you might use a size validation to limit the cost that a single file can add to your AWS bill, but if the file is stored before validations run, you incur its cost regardless.
So, I don't want to upload file to persist it after validation.
How can I retain the file without uploading file and saving blob?
I cannot use CarrierWave.