Active Storage:当表单重新显示时保留/缓存上传文件的最佳实践 [英] Active Storage: Best practice to retain/cache uploaded file when form redisplays
问题描述
使用 Active Storage 上传文件时,当文件上传完成并且表单重新显示时,例如当表单验证由于某种原因失败时,文件就会消失.
When uploading files with Active Storage, when a file is finished uploading and the form gets redisplayed, for example when the validation for that form fails for some reason, the file is gone.
有没有办法在表单重新显示之间缓存或保留它?Shrine 有一个很好的插件为此目的,我正在寻找类似的东西活动存储.
Is there a way to cache or retain it between form redisplays? Shrine has a nice Plugin for that purpose, I'm looking for something similar for Active Storage.
推荐答案
这是一个使 ActiveStorage 文件在表单重新显示时持久化的解决方案:<代码>f.hidden_field :image, value: f.object.image.signed_id 如果 f.object.image.attached?f.file_field :image
Here's a solution to make ActiveStorage files persist on form redisplay:
f.hidden_field :image, value: f.object.image.signed_id if f.object.image.attached?
f.file_field :image
这篇关于Active Storage:当表单重新显示时保留/缓存上传文件的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!