Rails 7'删除方法”和“没有路线匹配[GET]”

发布于 2025-02-02 17:21:06 字数 2412 浏览 3 评论 0原文

我有以下脚本删除草稿帖子

<%= link_to "Delete", api_post_path(draft), method: :delete, 
                                            remote: true %>

它可以正常工作,但是将Rails版本更新为7.0.3它不再工作了。




这些是我的项目信息,

  1. 我拥有库 collection
# app/controllers/libraries_controller.rb

class LibrariesController < ApplicationController
  ...
  def drafts
    @drafts = current_user.posts.recent.drafts.paginate(page: params[:page])
  end
  ...
end
  1. 我有此收藏的删除帖子草案
# 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
  1. 这是我的路线
# config/routes.rb

namespace :api do
  resources :posts, only: [:create, :update, :destroy]
end
  1. 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

  1. 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
  1. 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
  1. this is my routes
# config/routes.rb

namespace :api do
  resources :posts, only: [:create, :update, :destroy]
end
  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

药祭#氼 2025-02-09 17:21:06

如果要在Rails 7中使用链接删除某些内容,请简单注意:

<%= link_to 'Delete', api_post_path(draft),
  data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>

此链接将发送 real delete请求。

如果您的销毁操作以redirect_to结束(我怀疑是您的情况),则某些浏览器会以delete> delete method>方法重定向到新位置(引起错误),因此请确保添加状态:: SEE_OTHER参数到redirect_to,例如Guides

Just a quick note if you want to delete something with a link in rails 7:

<%= link_to 'Delete', api_post_path(draft),
  data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>

This link would send a real DELETE request.

If your destroy action ends with a redirect_to (which I doubt is your case), some browsers will redirect to a new location with DELETE method (causing errors), so make sure to add status: :see_other parameter to redirect_to, like the guides suggest.

皇甫轩 2025-02-09 17:21:06

使用以下内容

<%= link_to "Delete", api_post_path(draft), method: :delete, data: { turbo: false } %>

Use the following

<%= link_to "Delete", api_post_path(draft), method: :delete, data: { turbo: false } %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文