Rails Turbo 未呈现表单错误

问题描述 投票:0回答:1

我正在使用开箱即用的 Turbo 开发 Rails 7 项目。一般来说,我的所有导航都工作正常,但当表单出现错误时,我会遇到问题:

class ThingsController
  def update
    if @thing.update(thing_params)
      redirect_to @thing
    else
      render :edit
    end
  end
end

在这种情况下,表单无法重定向,我可以在服务器日志中看到错误,但不会触发我视图中的错误行为。我可以禁用表单的 Turbo,但我仍然想在这些页面上利用 Turbo。如何正确显示错误?

ruby-on-rails forms turbo
1个回答
0
投票

修复此问题只需对控制器中的渲染方法调用进行非常简单的更改:

class ThingsController
  def update
    if @thing.update(thing_params)
      redirect_to @thing
    else
      render :edit, status: :unprocessable_entity
    end
  end
end

:unprocessable_entity
标志添加到渲染调用中,让 Turbo 知道它需要重新渲染表单,从而正确渲染错误条件。

© www.soinside.com 2019 - 2024. All rights reserved.