是否有可能有条件地允许嵌套的属性? [英] Is it possible to conditionally allow nested attributes?

查看:146
本文介绍了是否有可能有条件地允许嵌套的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的形式​​,最终建立一个用户帐号,并完成它的一部分的个人资料。因此,用户belongs_to的个人资料。关键是,我想设置填充嵌套的属性配置文件,但只适用于这种形式。所有其他形式的用户模型,不应该允许发送嵌套的属性配置文件。这可能吗?怎么样?

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?

推荐答案

您可以轻松地有条件地拒绝嵌套属性的基于属性的通过传递的内容:reject_if accepts_nested_attributes_for

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.

有关你想达到什么,我根本无法接受的嵌套属性,而是调用 update_attributes的用户和控制器动作的轮廓上是新的用户窗体的目标。

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.

这篇关于是否有可能有条件地允许嵌套的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆