Removing ActiveStorage attachments in Rails 5











up vote
0
down vote

favorite












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









share|improve this question






















  • I have also tried using @upload = @product.upload.find(params[:id]) instead and it gives the same error.
    – Emily
    Nov 21 at 12:42










  • There's a pretty good answer here Rails 5.2 Active Storage purging/deleting attachements
    – Mike Heft
    Nov 21 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
    yesterday















up vote
0
down vote

favorite












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









share|improve this question






















  • I have also tried using @upload = @product.upload.find(params[:id]) instead and it gives the same error.
    – Emily
    Nov 21 at 12:42










  • There's a pretty good answer here Rails 5.2 Active Storage purging/deleting attachements
    – Mike Heft
    Nov 21 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
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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









share|improve this question













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






ruby-on-rails ruby-on-rails-5 rails-activestorage






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 11:26









Emily

277




277












  • I have also tried using @upload = @product.upload.find(params[:id]) instead and it gives the same error.
    – Emily
    Nov 21 at 12:42










  • There's a pretty good answer here Rails 5.2 Active Storage purging/deleting attachements
    – Mike Heft
    Nov 21 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
    yesterday


















  • I have also tried using @upload = @product.upload.find(params[:id]) instead and it gives the same error.
    – Emily
    Nov 21 at 12:42










  • There's a pretty good answer here Rails 5.2 Active Storage purging/deleting attachements
    – Mike Heft
    Nov 21 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
    yesterday
















I have also tried using @upload = @product.upload.find(params[:id]) instead and it gives the same error.
– Emily
Nov 21 at 12:42




I have also tried using @upload = @product.upload.find(params[:id]) instead and it gives the same error.
– Emily
Nov 21 at 12:42












There's a pretty good answer here Rails 5.2 Active Storage purging/deleting attachements
– Mike Heft
Nov 21 at 16:46




There's a pretty good answer here Rails 5.2 Active Storage purging/deleting attachements
– Mike Heft
Nov 21 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
yesterday




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
yesterday












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411099%2fremoving-activestorage-attachments-in-rails-5%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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.






    share|improve this answer

























      up vote
      0
      down vote













      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.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        Emily

        277




        277






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411099%2fremoving-activestorage-attachments-in-rails-5%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)