0

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
Emily
  • 97
  • 1
  • 12
  • I have also tried using @upload = @product.upload.find(params[:id]) instead and it gives the same error. – Emily Nov 21 '18 at 12:42
  • There's a pretty good answer here [Rails 5.2 Active Storage purging/deleting attachements](https://stackoverflow.com/questions/49515529/rails-5-2-active-storage-purging-deleting-attachements#answer-49517939) – Int'l Man Of Coding Mystery Nov 21 '18 at 16:46
  • Thanks - I had tried that and unfortunately couldn't get it to work for me, but I have finally got it working so have posted an answer. – Emily Nov 23 '18 at 09:45

1 Answers1

2

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.

Emily
  • 97
  • 1
  • 12