Home > Enterprise >  Rails ActiveStorage::Blob.find_signed purge rollback and don't delete files
Rails ActiveStorage::Blob.find_signed purge rollback and don't delete files

Time:09-14

I want to be able to delete pictures from my model attached to it.

I'm following the answer of this question https://stackoverflow.com/a/49517939/7295677

Here is my view :

<% @vente.image_vente.each do |image| %>
<tr>
    <th><%= image_tag(url_for(image), width: "150", height: "100") %></th>
    <th><%= link_to delete_image_attachment_gestion_vente_url(image.signed_id),class: "has-text-white", method: :delete, data: { confirm: "Voulez-vous vraiment supprimer cette image ?" } do%>
    <button >Supprimer l'image</button>
        <% end %>
     </th>
</tr>
<% end %>

Here is the method of my controller :

 def delete_image_attachment
  @image = ActiveStorage::Blob.find_signed(params[:id])
  puts "-------------------------------"
  @image.purge
  puts "-------------------------------"
  redirect_to action: "index"
end

And here is my route :

  resources :gestion_vente do
    member do
      delete :delete_image_attachment
    end
  end

The problem is that nothing get deleted and I got this :

TRANSACTION (0.1ms)  begin transaction
  ↳ app/controllers/gestion_vente_controller.rb:53:in `delete_image_attachment'
  ActiveStorage::Attachment Exists? (0.5ms)  SELECT 1 AS one FROM "active_storage_attachments" WHERE "active_storage_attachments"."blob_id" = ? LIMIT ?  [["blob_id", 66], ["LIMIT", 1]]
  ↳ app/controllers/gestion_vente_controller.rb:53:in `delete_image_attachment'
  TRANSACTION (0.1ms)  rollback transaction
  ↳ app/controllers/gestion_vente_controller.rb:53:in `delete_image_attachment'

I think that it rollback the transaction without error message

Edit :

As Corentin Bourdat said I could use :

ActiveStorage:: Attachment.find(params[:id]).destroy

But it left me with something I didn't understood. In my model I got

has_one_attached :image_principale
has_many_attached :image_vente

I want to only delete an occurence of :image_vente. The problem is that everytime it first delete my :image_principale

It's probably because by searching by id it doesn't care if the attachement is of :image_vente or :image_principale and I don't know how to do it

CodePudding user response:

Why don't you just simply use ActiveStorage::Attachment destroy instead of blobs purge ? Something like:

ActiveStorage:: Attachment.find(params[:id]).destroy

then add has_many_attached(dependent: :purge)to purge the blob immediately

CodePudding user response:

Found a solution for the problem that could help others :

If you have multiple attachement types like me, it won't forcefully query the right attachement, when I did this :

  @image = ActiveStorage::Attachment.find_by(params[:id])
  @image.purge
  redirect_to action: "index"

It deleted my :image_principale even if I specified the correct id of :image_vente

I corrected it by doing this instead

  @image = ActiveStorage::Attachment.find_by(name:"image_vente", id:params[:id])
  @image.purge
  redirect_to action: "index"
  • Related