Rails 5.2 Active Storage purging/deleting attachments

后端 未结 4 2095
醉话见心
醉话见心 2020-12-06 05:19

So I\'m using Active Storage to upload multiple images attached to a Collection model. Everything works well except whenever I\'m trying to purge/delete a single attachment

4条回答
  •  眼角桃花
    2020-12-06 06:00

    The following didn't work for me.

    def delete_image_attachment
      @image = ActiveStorage::Blob.find_signed(params[:id])
      @image.purge
      redirect_to collections_url
    end
    

    So what i did is found the attachment and purged it. You can do purge_later which is recommended.

    def delete_image_attachment
      @image = ActiveStorage::Blob.find_signed(params[:id])
      @image.attachments.first.purge
      redirect_to collections_url
    end
    

    This removed both attachment and blob record.

提交回复
热议问题