I need help to get delete of attachments to work from view with ActiveStorage
Hello, I am on Rails 5.2.3.
I just followed the rails guide on ActiveStorage, and have the attachments working. from a display, download link POV.
What I do not seem to get functioning is the delete of a single attachment via a link or button in my view. And then when the delete is done, i would it to refresh my view while staying on the same page.
Somehow with the code below, and likely the settings in my routes file, instead of going to delete_image it goes to destroy of the same controller, and of course can't do anything with what the parameter hands it.
I am not attached to the method chosen, as long as I can succeed in deleting the field from the view.
Any help is appreciated.
here is what I have in my view:
<% if @tr.image.attached? %>
<% @tr.image.each do |document| %>
<%= link_to document.blob.filename, url_for(document), target: :_blank %>
<%= image_tag url_for(document), size:100 %>
<%= link_to 'Download', document, download: document %>
<%= link_to 'Delete', delete_image_tr_path(document), method: :delete, data: { confirm: 'Are you sure?' }%>
<% end %>
<% end %>
in the controller I added :
def delete_image
@nowpath = params[:nowpath]
@image = ActiveStorage::Attachment.find(params[:id])
logger.debug "++++ in delete image --------------------" + @image.inspect + "---------------------"
@image.purge
redirect_to tr_path
end
and in my routes I have :
get '/tr/delete_image/:id', to: 'tr#delete_image', as: :delete_image_tr