错误在Rails 7中渲染编辑时未显示错误7
我的应用程序正在运行 Rails Rails 7.0.2.3
在我的更新控制器操作中,我有一行:
return render(:edit) unless @user_form.save
这会在错误时呈现编辑视图...但不会显示错误。
在我的编辑视图中,我使用以下方式定义表单:
form_for @user_form, url: user_path(@user_form), method: :patch do |f|
表单通过 Turbo 提交。我可以看到错误被添加到控制器中的 @user_form.errors
中,但视图中的 @user_form
实例在每次表单提交时都没有更改。如果我将 @user_form.inspect
输出到视图 - 每次提交时 ID 保持不变。
我尝试将 remote: false
添加到 form_for
调用中,但这似乎没有效果。
我找到的唯一解决方案是将 data: { Turbo: false }
添加到 form_for
调用中。
有更好的方法来处理这个问题吗?
My app is running Rails Rails 7.0.2.3
In my update controller action I have the line:
return render(:edit) unless @user_form.save
This renders the edit view on error .... but errors are not displayed.
In my edit view I am defining the form with:
form_for @user_form, url: user_path(@user_form), method: :patch do |f|
The form submits via turbo. I can see the error being added to @user_form.errors
in the controller, but the instance of @user_form
in the view is not changing on each form submission. If I output @user_form.inspect
to the view - the id remains the same on each submission.
I have tried adding remote: false
to the form_for
call, but this does not seem to have an effect.
The only solution I have found is to add data: { turbo: false }
to the form_for
call.
Is there a better way of handling this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用
状态渲染:: Unprocessable_entity
。我认为状态的变化使缓存无效。因此,将您的代码更改为:
You need to render with
status: :unprocessable_entity
. I think the change in status invalidates the cache.so change your code to:
您需要使用部分内容来显示错误。
您需要将
update.turbo_stream.erb
文件添加到该视图的目录中。在该文件中有类似的内容:或者您的控制器更新操作,您需要有类似的内容
,将所有这些更像伪代码并将其应用到您自己的应用程序中,如果没有看到您的代码,很难说发生了什么。
you'll want to use a partial to show the errors.
you'll need to add an
update.turbo_stream.erb
file to this view's directory. In that file have something like:or your controllers update action you'll want to have something like
Treat all this more like pseudocode and apply it to your own app, it's hard to say what's going on without seeing your code.