如何在 ActiveAdmin 中使用 ActiveStorage `has_many_attached` 编辑多个附加图像 [英] How to edit multiple attached images using ActiveStorage `has_many_attached` in ActiveAdmin
问题描述
我有一个简单的模型,可以通过 ActiveStorage
处理文件存储附加多个图像.
I have a simple model that can have multiple images attached via ActiveStorage
handling the file storage.
我正在使用 ActiveAdmin
来编辑我的模型并上传/附加图像 - 到目前为止没有问题.
I am using ActiveAdmin
to edit my model and to upload/attach the images - so far no problems.
问题是,当我想编辑我的模型并添加新图像时,之前的图像被删除,只添加了新图像.
The problem is, when I want to edit my model, and add new images, then the previous ones are deleted, and only the new ones added.
我可以预览已经附加的图片,也可以单独删除它们,但是我如何实现,通过上传新图片,旧图片不会被删除?
I can do a preview of already attached images, and could also delete them separately, but how do I achieve, that by uploading new images, the old ones are NOT deleted?
我的模型:
class Post < ActiveRecord::Base
has_many_attached :images
end
我的 ActiveAdmin 页面:
My ActiveAdmin page:
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
assuming u are using rails 6.0+; you can solve this by adding following code in to your environments (i.e - development.rb ) 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
这篇关于如何在 ActiveAdmin 中使用 ActiveStorage `has_many_attached` 编辑多个附加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!