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
ruby-on-rails ruby-on-rails-5 rails-activestorage
add a comment |
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
ruby-on-rails ruby-on-rails-5 rails-activestorage
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
add a comment |
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
ruby-on-rails ruby-on-rails-5 rails-activestorage
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
ruby-on-rails ruby-on-rails-5 rails-activestorage
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered yesterday
Emily
277
277
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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