Emily
Emily

Reputation: 97

Removing ActiveStorage attachments in Rails 5

I have an app with products - each product has things like notes/FAQs/attachments.

I can delete the notes & FAQs successfully, but not the Active Storage attachments.

Could somebody please assist? I've tried using a separate method in the Products controller but that didn't work, so my current line is using an Uploads controller.

The current error I am getting is:

NameError in UploadsController#destroy
uninitialized constant Upload

Uploads controller:

class UploadsController < ApplicationController
  load_and_authorize_resource :nested => :product       

    def destroy
        @product = Product.find(params[:id])
        @upload = @product.ActiveStorage::Attachment.find(params[:id])

        @upload.purge 

        redirect_back(fallback_location: products_path)
    end

end

Products view:

<% @product.uploads.each do |upload| %>
      <% if can? :destroy, upload %>
     <td><%= link_to t('X', :default => t("X")),
                      product_upload_path(@product, upload),
                      :method => :delete,
                      :data => { :confirm => t('.confirm', :default => 'Are you sure you want to delete this attachment?') },
                      :id =>'delete-faq' %></td>
      <% end %>
      <% if upload.variable? %>
        <span><%= image_tag upload.variant(resize: "100x100"), class: "other-image" %></span>
      <% elsif upload.previewable? %>
        <span><%= link_to image_tag(upload.preview(resize: "100x100"), class: "other-image"), rails_blob_path(upload), target: "_blank" %></span>
      <% else %>
        <span><%= link_to image_tag("paper.jpg", size: "100x100", class: "other-image"), rails_blob_path(upload), target: "_blank" %></span>
      <% end %>
    <% end %>

routes:

  resources :products do
    resources :notes
    resources :faqs
    resources :uploads 
  end

Upvotes: 0

Views: 6425

Answers (1)

Emily
Emily

Reputation: 97

I know this is not the most RESTful solution but I got it working, so am adding this as an answer in case it helps anybody else tearing their hair out.

I have checked the logs and it does remove both the attached image and the blob.

In my case, a product has_many uploads.

routes.rb:

resources :products do
    resources :uploads do
    match '/remove', to: 'products#remove', via: 'delete'
    end
  end

products controller (yes, I know, putting it here is not ideal)

def remove
        @product = Product.find(params[:product_id])
        @upload = @product.uploads.find(params[:upload_id])
        @upload.purge

        redirect_to product_path(@product), notice: "Upload was successfully removed."
    end

products show view:

 <% @product.uploads.each do |upload| %>
      <%= link_to "X", product_upload_remove_path(@product, upload),
                      :method => :delete,
                      :data => { :confirm => t('.confirm', :default => 'Are you sure you want to delete this upload?') },
                      :id =>'delete-faq', :class => 'delete' %></td>
 <% end %>

Hopefully this will help others or give a foundation for a more RESTful solution.

Upvotes: 2

Related Questions