使用 Hotwire 的 turbo-method: :delete 和 redirect_to 进行跳转。

huangapple go评论41阅读模式
英文:

Hotwire turbo with turbo-method: :delete and redirect_to

问题

My test erb:

<%= link_to 'Test', test_path, data: { turbo_method: :delete } %>

My test controller method looks like this:

def test
  redirect_to root_path, status: :see_other
end

Simple as that.. it responds with a turbostream and appends the response (the root page content) to my html tag instead of redirecting to the root page. Currently using "@hotwired/turbo-rails": "7.2.4".

Is that because turbo_method generates a form and executes inside it / misses the target? I tried several variations of adding a target but it always appends the response..

Shouldn't `turbo_method: :delete` be the same as rails ujs `method: :delete`?
英文:

My test erb:

<%= link_to 'Test', test_path, data: { turbo_method: :delete } %>

My test controller method looks like this:

def test
  redirect_to root_path, status: :see_other
end

Simple as that.. it responds with a turbostream and appends the response (the root page content) to my html tag instead of redirecting to the root page. Currently using "@hotwired/turbo-rails": "7.2.4"

Is that because turbo_method generates a form and executes inside it / misses the target? I tried several variations of adding a target but it always appends the response..

Shouldn't turbo_method: :delete be the same as rails ujs method: :delete?

答案1

得分: 0

我找到了问题。当view方法被重定向到时,如果没有为HTML声明respond_to格式,或者HTML模板的文件名中没有包含"html",比如index.slimturbo_stream就不知道如何处理它,而是附加它而不是替换整个页面。

英文:

I found the problem. when the view method which it gets redirected to - does not have a declared respond_to format for html or the html template has no html in the file name like index.slim turbo_stream does not know how to handle it and it appends it instead of replacing the whole page.

huangapple
  • 本文由 发表于 2023年2月14日 00:24:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定