简体   繁体   中英

Rails active storage issue with deleting individual uploads

I am saving some files to local storage via active storage. I am displaying them as links on my page. They could be pdfs, images, word docs etc. I want to have a link next to it to be able to delete the individual files but haven't gotten it to work yet. Here is what I have:

Projects Model:

class Project < ApplicationRecord

belongs_to :user

has_one :proj_status, :foreign_key => "status_id", :primary_key => 'status_id'

has_many_attached :uploads

end

Projects Show Page:

<%= link_to upload.filename, rails_blob_path(upload, disposition: :attachment) %>

<%= link_to 'Remove', delete_upload_project_url(upload.id), method: :delete, data: { confirm: 'Are you sure?' } %>

Projects Controller delete upload method:

def delete_upload

attachment = ActiveStorage::Attachment.find(params[:id])

attachment.purge # or use purge_later

redirect_back(fallback_location: projects_path)

end

I get the error:

No route matches [GET] "/projects/1/delete_upload"

I am thinking this is a routing issue but I haven't been able to get it to work yet.

Also here is my routes section:

resources :projects do

  member do

  delete :delete_upload

  end

end

Any thoughts or ideas on how to get this working?

Here is my rake routes for the projects section:

DELETE /projects/:id/delete_upload/:upload_id(.:format) projects#delete_upload

projects GET /projects(.:format) projects#index

POST /projects(.:format) projects#create

new_project GET /projects/new(.:format) projects#new

edit_project GET /projects/:id/edit(.:format) projects#edit

project GET /projects/:id(.:format) projects#show

PATCH /projects/:id(.:format) projects#update

PUT /projects/:id(.:format) projects#update

DELETE /projects/:id(.:format) projects#destroy

If anyone can help me with this I would appreciate it!

Your delete_upload_project_url method expect two variables: a project and an upload /projects/:id/delete_upload/:upload_id . You are only passing the upload id. Do something like delete_upload_project_url(project.id, upload.id) .

Everything was set up correctly from the get go. The problem was that I had accidentally removed the include for application.js from my project. The lack of UJS reference was causing all requests to be gets instead of the delete in my case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM