错误在Rails 7中渲染编辑时未显示错误7

发布于 2025-01-18 03:36:07 字数 653 浏览 4 评论 0原文

我的应用程序正在运行 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 技术交流群。

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

发布评论

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

评论(2

惯饮孤独 2025-01-25 03:36:07

您需要使用状态渲染:: Unprocessable_entity。我认为状态的变化使缓存无效。

因此,将您的代码更改为:

return render(:edit, status: :unprocessable_entity) unless @user_form.save

You need to render with status: :unprocessable_entity. I think the change in status invalidates the cache.

so change your code to:

return render(:edit, status: :unprocessable_entity) unless @user_form.save
貪欢 2025-01-25 03:36:07

您需要使用部分内容来显示错误。

您需要将 update.turbo_stream.erb 文件添加到该视图的目录中。在该文件中有类似的内容:

<%= turbo_stream.update 'id_of_div_where_you_want_to_show_errors' do %>
   <%= render partial: 'partial_that_displays_errors' %>
<% end %>

或者您的控制器更新操作,您需要有类似的内容

respond_to do |format|
  format.turbo_stream { turbo_stream.update 'id_of_div_where_you_want_to_show_errors', render partial: 'partial_that_displays_errors' %>
end

,将所有这些更像伪代码并将其应用到您自己的应用程序中,如果没有看到您的代码,很难说发生了什么。

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:

<%= turbo_stream.update 'id_of_div_where_you_want_to_show_errors' do %>
   <%= render partial: 'partial_that_displays_errors' %>
<% end %>

or your controllers update action you'll want to have something like

respond_to do |format|
  format.turbo_stream { turbo_stream.update 'id_of_div_where_you_want_to_show_errors', render partial: 'partial_that_displays_errors' %>
end

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.

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