You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails中Bookmark删除报错:No route matches [GET] "/bookmarks/11"排查

问题原因及解决方案

核心问题

报错No route matches [GET] "/bookmarks/11"说明删除请求用了GET方法,但Rails路由中bookmarks#destroy仅接受DELETE方法。你虽然添加了data: { turbo_method: :delete },但Turbo的方法转换功能未生效。

修复步骤

  1. 确认Turbo已正确导入
    检查app/javascript/application.js是否包含以下导入语句:

    import "@hotwired/turbo-rails"
    

    若缺失则添加,之后重启服务器。Turbo是Rails7默认前端栈的核心组件,必须加载才能让data-turbo-method生效。

  2. 改用button_to(备选方案)
    如果Turbo加载后问题仍存在,可替换link_to为button_to——它默认生成表单发送DELETE请求,无需依赖Turbo的方法转换:

    <%= button_to "delete", bookmark_path(bookmark), method: :delete, data: { turbo_confirm: "Are you sure you want to remove #{movie.title} from your #{@list.name} list"}, class: 'btn btn-link text-danger p-0' %>
    

    可调整class样式让按钮外观更接近链接。

  3. 排查Turbo全局禁用情况
    确保页面或布局中没有添加data-turbo="false"属性,该属性会阻止Turbo处理请求。

  4. 验证bookmark对象有效性
    虽然报错路径显示id=11存在,但仍建议确认Bookmark.find_by(list: @list, movie: movie)未返回nil,避免生成无效路径:

    <% if bookmark %>
      <%= link_to "delete", bookmark_path(bookmark), data: { turbo_method: :delete, turbo_confirm: "Are you sure you want to remove #{movie.title} from your #{@list.name} list"}, class: 'text-danger' %>
    <% end %>
    

结论

这不是Rails7的问题,是Turbo未正确加载或请求方法转换失效导致的配置问题。

内容的提问来源于stack exchange,提问作者mustafaalomer711

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 03:01:39