Rails:没有“field_with_errors”当使用“fields_for”时
所以,我的“用户”模型中有这个嵌套的“组织”模型。 用户模型是基于设计的模型,组织属于用户。
我为它创建了一个表单,一切正常。 甚至是“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也有同样的问题。使用这个就可以了:
注意
user.organization ||
部分:如果组织已经构建,你不应该重新构建它,否则你将丢失你的错误并且 field_with_errors-div 获胜不出现。I had the same problem. Use this and it works:
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.我也遇到了同样的问题,我通过将
validates_linked :child_model
添加到 parent 模型来修复它。I also had the same problem and I fixed it by adding
validates_associated :child_model
to parent model.