我有一个简单的模型,可以通过附加多个图像ActiveStorage
处理文件存储。
我在用ActiveAdmin
编辑我的模型并上传/附加图像 - 到目前为止没有问题。
问题是,当我想编辑模型并添加新图像时,以前的图像会被删除,只添加新图像。
我可以预览已附加的图像,也可以单独删除它们,但是如何实现通过上传新图像而不删除旧图像?
我的型号:
class Post < ActiveRecord::Base
has_many_attached :images
end
我的 ActiveAdmin 页面:
ActiveAdmin.register AdminPost do
permit_params images:[]
form do |f|
f.input :images, as: :file, input_html: { multiple: true }
if @resource.images.exists?
@resource.images.map do |m|
para image_tag m
end
end
end
end
假设您使用的是 Rails 6.0+;
您可以通过在您的环境中添加以下代码来解决此问题(即 -development.rb
)
https://github.com/rails/rails/issues/35817#issuecomment-628654948 https://github.com/rails/rails/issues/35817#issuecomment-628654948
config.active_storage.replace_on_assign_to_many = false
以你的形式,
form do |f|
f.input :images, as: :file, input_html: { multiple: true }
f.object.images.each do |image|
span image_tag(image)
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)