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

Rails7中销毁嵌套资源的link_to为何同时尝试删除父资源?

Rails7中销毁关联模型后意外触发父模型DELETE请求的问题

我有两个模型:site和heading,site与heading为一对多(has_many)关联。我编写了如下销毁链接代码:

<%= link_to site_heading_path(@site, @heading), data: { 'turbo-method': :delete, 'turbo-confirm': "Are you sure?"} do %>
  <i class="ti ti-trash"></i> Destroy
<% end %>

headings控制器的destroy方法为默认生成的代码:

def destroy
  @heading.destroy

  respond_to do |format|
    format.html { redirect_to site_path(@site), notice: "Heading was successfully destroyed." }
    format.json { head :no_content }
  end
end

这段代码我曾多次编写,但在全新的Rails7基础项目中出现异常。

预期行为

删除heading后,重定向到/sites/1

实际行为

  • heading被删除(正常)
  • 重定向生效(正常)
  • TURBO_STREAMS意外调用/sites/1的DELETE请求(异常)

我不清楚该问题产生的原因,webrick输出显示系统尝试删除site——请求记录中出现针对/sites/1的DELETE请求,发起者为Turbo Streams。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 15:09:12