我正在尝试找到一种方法来删除使用activestorage从activeadmin上传的图像。我设法在显示视图和资源编辑中显示所有图像,但是如果需要,我似乎找不到删除每个图像的方法。我的型号代码:
class Category < ApplicationRecord
has_many_attached :images
end
和活动管理员资源文件:
ActiveAdmin.register Category do
permit_params :category_name, :description, :photo_cover, images: []
index do
selectable_column
id_column
column :category_name
column :created_at
actions
end
show do |t|
if t.photo_cover.attached? && t.images.attached?
attributes_table do
row :category_name
row :description
row "images" do |m|
m.images.each do |img|
span do
image_tag(img)
end
end
end
end
else
attributes_table do
row :category_name
row :description
end
end
end
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Basic Info" do
f.input :category_name
f.input :description
end
f.inputs do
f.input :images, as: :file, input_html: { multiple: true }
end
if f.object.images.attached?
f.object.images.each do |img|
span do
image_tag(img)
end
end
end
f.actions
end
end
提前谢谢你!!
在 ActiveAdmin 模型中创建自定义路由,如此处所述
member_action :delete_category_image, method: :delete do
@pic = ActiveStorage::Attachment.find(params[:id])
@pic.purge_later
redirect_back(fallback_location: edit_admin_category_path)
end
在form do
,
f.object.images.each do |img|
li class: "some_class" do
figure do
img src: rails_representation_path(img.variant(resize_to_fit: [200, 200])), alt: "Photo"
figcaption img.blog.filename
end
a "Delete", src: delete_category_image_admin_category_path(img.id), "data-method": :delete, "data-confirm": "Are you sure?"
end
end