Ruby on Rails - 错误后保持所选图像的格式

Ruby on Rails - Keep selected images in form after error

我在我的应用程序中使用 simple_form 处理表单,使用 CarrierWave gem 处理 图片上传

有时在提交表单时,CarrierWave 会给出一些关于图像的错误,例如:(文件看起来是另一种格式等).

出错后,我在 file_field :image 字段中的所有 selected 图像都得到 empty/null,我必须再次 select 它们一张一张。

我通常有 30 多个嵌套表单和 30 多个图片上传,我使用 nested_forms 添加每个图片

如果表单有一些错误,我如何防止表单出现 empty/null file_field :image 字段。

您需要添加一个名为 image_cache

的隐藏字段
<`%= f.hidden_field :image_cache %>`

您可以在 Carrierwave Github page 上找到此信息。