我正在尝试找到一种使用activestorage删除从activeadmin上传的图像的方法。我设法在show视图和资源编辑中显示了所有图像,但是我似乎找不到想删除每个图像的方法。我的模型代码:
class Category < ApplicationRecord
has_many_attached :images
end
和activeadmin资源文件:
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
提前谢谢!
答案 0 :(得分:1)
在active_admin模型中以here创建自定义路由
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
形式为
if f.object.images.attached?
f.object.images.each do |img|
div: class=> "some_class" do
span image_tag(img)
span link_to "delete",delete_category_image_admin_category_path(img.id),method: :delete,data: { confirm: 'Are you sure?' }
end
end
end
end