Rails 7+ Turbo:收到422个无法取得的实体时,Turbo会导航

发布于 2025-02-07 23:00:52 字数 700 浏览 3 评论 0 原文

根据各种消息来源,在Rails 7中未能表单验证的方法是响应状态422无法处理的实体。但是,就我而言,这会导致Turbo发出另一个获取请求,这不太可能是预期的结果。

日志看起来如下:

Started POST "/users/new" for 127.0.0.1 at 2022-06-16 17:27:48 +0200
...
TRANSACTION (0.3ms)  ROLLBACK
...
Rendered html template (Duration: 0.0ms | Allocations: 4)
Completed 422 Unprocessable Entity in 133ms (Views: 1.2ms | ActiveRecord: 10.9ms | Allocations: 54437)

Started GET "/users/new" for 127.0.0.1 at 2022-06-16 17:27:48 +0200
...

因此,从铁路的角度来看,一切似乎正常,因为服务器以所需的422状态代码做出响应。在检查浏览器中的响应时,我会看到带有错误消息的表单。但是,由于某种原因,Turbo然后将重定向到/users/new ,而不是让浏览器显示包含错误的表单。

更新:该表格是使用 simple_form_for 生成的。

我如何找出为什么涡轮增压到页面?

最好的, 卡尔森

According to various sources, the way to fail form validation in Rails 7 is to respond with status 422 unprocessable entity. However, in my case, this causes Turbo to issue yet another GET request, which is unlikely to be the desired outcome.

The log looks as follows:

Started POST "/users/new" for 127.0.0.1 at 2022-06-16 17:27:48 +0200
...
TRANSACTION (0.3ms)  ROLLBACK
...
Rendered html template (Duration: 0.0ms | Allocations: 4)
Completed 422 Unprocessable Entity in 133ms (Views: 1.2ms | ActiveRecord: 10.9ms | Allocations: 54437)

Started GET "/users/new" for 127.0.0.1 at 2022-06-16 17:27:48 +0200
...

So, from a Rails perspective, everything seems normal, as the server responds with the required 422 status code. When inspecting the response in the browser, I see the form with the error messages. However, for some reason, Turbo then issues a redirect to /users/new instead of letting the browser display the form containing the errors.

UPDATE: The form is generated using simple_form_for.

How can I find out why Turbo reloads the page?

Best,
Kalsan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃气十足 2025-02-14 23:00:52

当服务器对表单帖子的部分响应时,就会发生这种情况。返回一个完整的站点,包括< html> 等。解决了问题。

This happens when the server responds with a partial to a form post. Returning a complete site, including <html> etc. solves the problem.

吝吻 2025-02-14 23:00:52

在这里远非铁路专家,但我已经看到涡轮增压表提交了422:Unprocessable_entity的回复,然后立即获得响应200的起始URL。它是令人难以置信的无证Afaik。我在这里要求提供官方文档帮助:

https://discuss.rubyonrails.org/t/do-we-we-need-need-rails-rails-7-7-7-guides-for-for--------------7-guides-for-intergrating-with-turbo--turbo--turbo--turbo--turbo--turbo--turbo----------- to-Counter-stackoverflow-cruft/83635

当涡轮在响应中找不到完整的DOM时,就会发生 它发生。它无法比较您所拥有的东西,因此只会踩踏422并获得。我已经看到这种情况在两种情况下发生:呈现部分而不是呈现完整的响应,或者从没有布局的控制器呈现。

原因1:渲染部分而不是完全渲染

以下内容会导致惊喜获得200,使您的422 Unrprocessable_entity踩踏:

# with app/views/_book.html.haml
render partial: 'book'
return
render partial: 'book', status: :unprocessable_entity
return

惊喜!您可能会认为涡轮的魔力将能够整齐地识别您的部分替代品,但不能。您可能会认为这会引起例外,但事实并非如此。

另一方面,这将起作用:

# app/views/book.html.haml
render 'book', status: :unprocessable_entity

现在,您正在呼吁进行完整的响应和422杆。

文档描述渲染是一个完整的响应:

原因2:使用没有布局的控制器

,有一种选择布局的方法,而轨道显然可以选择没有布局或错误的布局。

如果您没有“ more_different_book”布局:

# controllers/more_different_book_controller.rb
# no layout called for
def do_stuff
  ...
  return render 'book', status: :unprocessable_entity
end

获得200!

但是,添加明确的(现有)布局可以神奇地修复它:

# controllers/more_different_book_controller.rb
layout 'book'
def do_stuff
  ...
  return render 'book', status: :unprocessable_entity
end

我可能会误解原因,但这是我观察到的以及如何超越它。

Far from a Rails expert here, but I have seen a Turbo form submit, get a response of 422 :unprocessable_entity and then immediately GET the starting url with response 200. It's maddeningly undocumented afaik. I've asked for official documentation help here:

https://discuss.rubyonrails.org/t/do-we-need-official-rails-7-guides-for-integrating-with-turbo-to-counter-stackoverflow-cruft/83635

It happens when Turbo cannot find a complete DOM in the response. It can't compare what you have to what you're getting and so it just stomps the 422 and GETs. I have seen this happen in two circumstances: rendering a partial instead of rendering a complete response, or rendering from a controller without a layout.

Cause 1: Rendering a partial instead of fully rendering

The following will result in a surprise GET 200 that stomps your 422 unprocessable_entity:

# with app/views/_book.html.haml
render partial: 'book'
return
render partial: 'book', status: :unprocessable_entity
return

Surprise! You would think the magic of Turbo would be able to neatly identify your partial for replacement, but it cannot. You'd think it would raise an exception, but it does not.

This on the other hand will work:

# app/views/book.html.haml
render 'book', status: :unprocessable_entity

Now you're calling for a complete response and the 422 sticks.

Documentation describing render as a complete response:

https://guides.rubyonrails.org/layouts_and_rendering.html#creating-responses

Cause 2: Using a controller without a layout

There's a method for selecting your layout and Rails can select no layout or the wrong layout, apparently.

https://guides.rubyonrails.org/layouts_and_rendering.html#finding-layouts

This could be wrong if you don't have a "more_different_book" layout:

# controllers/more_different_book_controller.rb
# no layout called for
def do_stuff
  ...
  return render 'book', status: :unprocessable_entity
end

GET 200!

But adding an explicit (existing) layout could magically fix it:

# controllers/more_different_book_controller.rb
layout 'book'
def do_stuff
  ...
  return render 'book', status: :unprocessable_entity
end

I could be misunderstanding the cause, but this is what I have observed and how I moved past it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文