【问题标题】:Activeadmin - activestorage delete edit multiple photosActiveadmin - activestorage 删除编辑多张照片
【发布时间】:2018-08-31 10:48:08
【问题描述】:

我正在尝试找到一种方法来删除使用 activestorage 从 activeadmin 上传的图像。我设法在显示视图中显示所有图像并编辑资源,但我似乎无法找到删除每个图像的方法。我的型号代码:

  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

提前谢谢你!!

【问题讨论】:

    标签: ruby-on-rails activeadmin rails-activestorage


    【解决方案1】:

    按照here 的说明在 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
    

    【讨论】:

    • 其实应该是a "Delete", href: delete...而不是a "Delete", src: delete,因为它是一个a标签
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    相关资源
    最近更新 更多