【发布时间】:2022-07-02 23:13:15
【问题描述】:
我有以下用于删除草稿帖子的脚本
<%= link_to "Delete", api_post_path(draft), method: :delete,
remote: true %>
它可以正常工作,但是在将 rails 版本更新为 7.0.3 后,它就不再工作了。
这些是我的项目信息
- 我有图书馆收藏
# 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
- 查看以显示所有草稿帖子列表以及删除草稿帖子的链接
<!-- 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 } %> -
我补充说这是一个答案,因为它可能对其他人有用。
标签: ruby-on-rails routes