Rails 7 "delete method" 和 "No route matches [GET]"

Rails 7 "delete method" and "No route matches [GET]"

我有以下用于删除草稿的脚本 posts

<%= 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. 我有这个 collection 用于 删除草稿 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. 这是我的路线
# config/routes.rb

namespace :api do
  resources :posts, only: [:create, :update, :destroy]
end
  1. 查看显示所有草稿post列表,link删除草稿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();

但现在它不起作用,然后我删除了 method: :delete 并更新了新脚本

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

它仍然无法正常工作,然后我通过删除 remote: true

再次更新了脚本
<%= link_to "Delete", api_post_path(draft), data: { 
                                                    turbo_method: "delete", 
                                                    turbo_confirm: "Are you sure?" 
                                            } %>

之后,我得到了这个错误

No route matches [GET] "/api/posts/xxx"

请告诉我如何解决这个问题

使用以下内容

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