Rails:没有“field_with_errors”当使用“fields_for”时

发布于 2024-11-09 05:19:03 字数 574 浏览 0 评论 0原文

所以,我的“用户”模型中有这个嵌套的“组织”模型。 用户模型是基于设计的模型,组织属于用户。

我为它创建了一个表单,一切正常。 甚至是“devise_error_messages!”正确输出一切。

唯一的问题是本地化和 field_with_errors-div ..

我通过在我的语言环境文件中执行此操作成功地实现了本地化:

nl:
  activerecord:
    attributes:
      user:
        contact:
          account_number: "bankrekening nummer"

当然,我只是希望它能够在“用户”列表中没有“联系人”的情况下工作。

但我在这里遇到的严重问题是 'field_with_errors' div 女巫通常包裹在有错误的字段周围..我用它在有错误的字段上创建红色标签..它们为用户类工作,但他们赢了不适用于嵌套表单。

谷歌没有提供任何帮助,就像除了我之外没有人遇到这个问题。

这可能吗?

如果是的话,你能指出我这样做的正确方向吗?

谢谢!

So, I have this nested 'organization' model in my 'user' model.
The user model is a devise based model and the organization belongs_to user.

I created a form for it, and everything works fine.
Even the 'devise_error_messages!' output everything correctly.

Only problems are localization and the field_with_errors-div..

I managed to get localization right by doing this in my locale file:

nl:
  activerecord:
    attributes:
      user:
        contact:
          account_number: "bankrekening nummer"

Of course I just want it to work without 'contact' being in the 'user' list.

But the serious problem I'm having here is the 'field_with_errors' div witch is normally wrapped around the fields with errors.. I use it to create red colored labels on fields with errors.. They work for the user class, but they won't work for the nested forms..

Google isn't helping anything, like no one is having this issue except for me..

Is this even possible?

If it is, can you point me in the right direction of doing this?

Thanks!

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

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

发布评论

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

评论(2

今天小雨转甜 2024-11-16 05:19:03

我也有同样的问题。使用这个就可以了:

<%= f.fields_for :organization, (user.organization || user.build_organization) do |organization_form|
  ...
<% end %>

注意 user.organization || 部分:如果组织已经构建,你不应该重新构建它,否则你将丢失你的错误并且 field_with_errors-div 获胜不出现。

I had the same problem. Use this and it works:

<%= f.fields_for :organization, (user.organization || user.build_organization) do |organization_form|
  ...
<% end %>

Note the user.organization || part: if the organization has been already built, you shouldn't re-build it or you will lose your errors and the field_with_errors-div won't appear.

似最初 2024-11-16 05:19:03

我也遇到了同样的问题,我通过将 validates_linked :child_model 添加到 parent 模型来修复它。

I also had the same problem and I fixed it by adding validates_associated :child_model to parent model.

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