我正在通过活动存储将某些文件保存到本地存储。我将它们显示为页面上的链接。它们可以是pdf,图像,word文档等。我希望在其旁边有一个链接,以便能够删除单个文件,但尚未使它起作用。这是我所拥有的:
项目模型:
class Project < ApplicationRecord
belongs_to :user
has_one :proj_status, :foreign_key => "status_id", :primary_key => 'status_id'
has_many_attached :uploads
end
项目显示页面:
<%= 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删除上传方法:
def delete_upload
attachment = ActiveStorage::Attachment.find(params[:id])
attachment.purge # or use purge_later
redirect_back(fallback_location: projects_path)
end
我得到了错误:
没有路线与[GET]“ / projects / 1 / delete_upload”匹配
我认为这是一个路由问题,但是我还无法使其正常工作。
这也是我的路线部分:
resources :projects do
member do
delete :delete_upload
end
end
关于如何使它起作用的任何想法或想法?
这是我在专案专区的耙路:
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
如果有人可以帮助我,我将不胜感激!
答案 0 :(得分:0)
您的delete_upload_project_url
方法需要两个变量:一个项目和一个上载/projects/:id/delete_upload/:upload_id
。您只传递上传ID。做类似delete_upload_project_url(project.id, upload.id)
的事情。
答案 1 :(得分:0)
一切从一开始就已正确设置。问题是我不小心从项目中删除了application.js的包含。在我的情况下,缺少UJS参考导致所有请求都被获取而不是删除。