gpt4 book ai didi

ruby-on-rails - 为什么这个 Rails 7 link_to destroy 嵌套资源也试图破坏父资源?

转载 作者:行者123 更新时间:2023-12-05 05:37:11 24 4
gpt4 key购买 nike

我有两个模型:siteheading,site has_many headings.

我有一个销毁链接:


<%= 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 %>

这是我的标题 Controller 方法,它基本上只是库存操作:

  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

这段代码我已经写过一千遍了,但出于某种原因,在我的准系统新 Rails 7 代码库中,它做了一些奇怪的事情。

预期:

标题被删除,我被重定向到/sites/1

实际行为:

  1. 标题已删除(好)
  2. 我可以看到重定向(好)
  3. TURBO_STREAMS 出于某种原因在 /sites/1 上调用 DELETE(非常糟糕)

我不知道为什么会这样。

这是 webrick 的输出。您可以看到它也试图删除该站点。我不知道为什么..

enter image description here

最佳答案

这不是新问题,这是 2013 年的问题:

Rails Redirect After Delete Using DELETE Instead of GET

但是,它变得与 Turbo 非常相关。当从 javascript DELETE 请求重定向时,浏览器将使用 DELETE 作为重定向位置。解决方案是使用 303 重定向,它始终是 GET 请求:

redirect_to site_path(@site), status: 303
redirect_to site_path(@site), status: :see_other

我猜这算作记录:

https://turbo.hotwired.dev/handbook/drive#redirecting-after-a-form-submission

After a stateful request from a form submission, Turbo Drive expectsthe server to return an HTTP 303 redirect response, which it will thenfollow and use to navigate and update the page without reloading.

关于ruby-on-rails - 为什么这个 Rails 7 link_to destroy 嵌套资源也试图破坏父资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73172729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com