简体   繁体   English

删除单个上载的Rails活动存储问题

[英]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. 它们可以是pdf,图像,word文档等。我希望在其旁边有一个链接,以便能够删除单个文件,但尚未使它起作用。 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: Projects Controller删除上传方法:

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" 没有路线匹配[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 . 您的delete_upload_project_url方法需要两个变量:一个项目和一个上载/projects/:id/delete_upload/:upload_id You are only passing the upload id. 您只传递上传ID。 Do something like delete_upload_project_url(project.id, upload.id) . 做类似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. 问题是我不小心从项目中删除了application.js的包含。 The lack of UJS reference was causing all requests to be gets instead of the delete in my case. 在我的情况下,缺少UJS参考导致所有请求都被获取而不是删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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