Is it possible to conditionally allow nested attributes?

大憨熊 提交于 2019-12-11 07:41:43

问题


I have one big form, which ends up creating a User account, and completing part of it's profile. So, User belongs_to Profile. The thing is, I'd like to set the profile to be filled as nested attributes, but only for this form. All other forms for the User model should not allow sending nested attributes for the profile. Is this possible? How?


回答1:


You can easily conditionally reject nested attributes based on the contents of the attributes by passing a :reject_if block to accepts_nested_attributes_for.

For what you're trying to achieve, I'd simply not accept nested attributes, and instead call update_attributes on both the user and the profile in the controller action that is the target of the new user form.



来源:https://stackoverflow.com/questions/12520710/is-it-possible-to-conditionally-allow-nested-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!