Rails 7'删除方法”和“没有路线匹配[GET]”
我有以下脚本删除草稿帖子
<%= link_to "Delete", api_post_path(draft), method: :delete,
remote: true %>
它可以正常工作,但是将Rails版本更新为7.0.3
它不再工作了。
这些是我的项目信息,
- 我拥有库 collection
# app/controllers/libraries_controller.rb
class LibrariesController < ApplicationController
...
def drafts
@drafts = current_user.posts.recent.drafts.paginate(page: params[:page])
end
...
end
- 我有此收藏的删除帖子草案
# app/controllers/api/posts_controller.rb
module Api
class PostsController < ApplicationController
...
destroy
@post = current_user.posts.friendly.find(params[:id])
@post.destroy
end
...
end
end
- 这是我的路线
# config/routes.rb
namespace :api do
resources :posts, only: [:create, :update, :destroy]
end
- view 要显示所有草稿帖子列表,其中包含删除草案帖子的链接
<!-- app/views/libraries/drafts.html.erb -->
<div id="library_<%= draft.id %>">
...
<%= link_to "Delete", api_post_path(draft), method: :delete,
remote: true %>
...
</div>
<!-- app/views/api/posts/destroy.js.erb -->
$('#library_<%= @post.id %>').fadeOut();
,但是现在它不起作用,然后我删除了方法:: delete
并更新了新脚本,
<%= link_to "Delete", api_post_path(draft), data: {
turbo_method: "delete",
turbo_confirm: "Are you sure?"
},
remote: true %>
它仍然无法正常工作,然后我再次更新了脚本通过删除远程:true
<%= link_to "Delete", api_post_path(draft), data: {
turbo_method: "delete",
turbo_confirm: "Are you sure?"
} %>
之后,我得到了此错误,
No route matches [GET] "/api/posts/xxx"
请建议我如何解决此问题
I have the below script for deleting the draft posts
<%= link_to "Delete", api_post_path(draft), method: :delete,
remote: true %>
It works properly, but after updating the rails version to 7.0.3
it's not working anymore.
These are my project information
- I have the Libraries collection
# app/controllers/libraries_controller.rb
class LibrariesController < ApplicationController
...
def drafts
@drafts = current_user.posts.recent.drafts.paginate(page: params[:page])
end
...
end
- I have this collection for deleting the draft post
# app/controllers/api/posts_controller.rb
module Api
class PostsController < ApplicationController
...
destroy
@post = current_user.posts.friendly.find(params[:id])
@post.destroy
end
...
end
end
- this is my routes
# config/routes.rb
namespace :api do
resources :posts, only: [:create, :update, :destroy]
end
- View to show all draft posts list with the link for deleting the draft post
<!-- app/views/libraries/drafts.html.erb -->
<div id="library_<%= draft.id %>">
...
<%= link_to "Delete", api_post_path(draft), method: :delete,
remote: true %>
...
</div>
<!-- app/views/api/posts/destroy.js.erb -->
$('#library_<%= @post.id %>').fadeOut();
But now it's not working then I removed method: :delete
and updated the new script
<%= link_to "Delete", api_post_path(draft), data: {
turbo_method: "delete",
turbo_confirm: "Are you sure?"
},
remote: true %>
It still not working then I updated the script again by removing remote: true
<%= link_to "Delete", api_post_path(draft), data: {
turbo_method: "delete",
turbo_confirm: "Are you sure?"
} %>
After that, I got this error
No route matches [GET] "/api/posts/xxx"
Please advise how can I fix this issue
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要在Rails 7中使用链接删除某些内容,请简单注意:
此链接将发送 real
delete
请求。如果您的
销毁
操作以redirect_to
结束(我怀疑是您的情况),则某些浏览器会以delete> delete
method>方法重定向到新位置(引起错误),因此请确保添加状态:: SEE_OTHER
参数到redirect_to
,例如GuidesJust a quick note if you want to delete something with a link in rails 7:
This link would send a real
DELETE
request.If your
destroy
action ends with aredirect_to
(which I doubt is your case), some browsers will redirect to a new location withDELETE
method (causing errors), so make sure to addstatus: :see_other
parameter toredirect_to
, like the guides suggest.使用以下内容
Use the following