Accepts_nested_attributes_for 是否可以与Belongs_to一起使用?
关于这个基本问题,我得到了各种相互矛盾的信息,而答案对于我当前的问题至关重要。那么,很简单,在Rails 3中,是否允许将accepts_nested_attributes_for与belongs_to关系一起使用?
class User < ActiveRecord::Base
belongs_to :organization
accepts_nested_attributes_for :organization
end
class Organization < ActiveRecord::Base
has_many :users
end
在一个视图中:
= form_for @user do |f|
f.label :name, "Name"
f.input :name
= f.fields_for :organization do |o|
o.label :city, "City"
o.input :city
f.submit "Submit"
I have been getting all kinds of conflicting information regarding this basic question, and the answer is pretty crucial to my current problems. So, very simply, in Rails 3, is it allowed or not allowed to use accepts_nested_attributes_for with a belongs_to relationship?
class User < ActiveRecord::Base
belongs_to :organization
accepts_nested_attributes_for :organization
end
class Organization < ActiveRecord::Base
has_many :users
end
In a view:
= form_for @user do |f|
f.label :name, "Name"
f.input :name
= f.fields_for :organization do |o|
o.label :city, "City"
o.input :city
f.submit "Submit"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 Rails 4 开始,嵌套属性似乎对于“belongs_to”关联工作得很好。它可能在早期版本的 Rails 中发生了更改,但我在 4.0.4 中进行了测试,它确实按预期工作。
Nested attributes appear to work fine for a belongs_to association as of Rails 4. It might have been changed in an earlier version of Rails, but I tested in 4.0.4 and it definitely works as expected.
epochwolf 引用的文档在第一行中指出“嵌套属性允许您通过父级在关联记录上保存属性。” (我的重点)。
您可能对这另一个 SO 问题与这个问题相同。它描述了两种可能的解决方案:1)将accepts_nested_attributes移至关系的另一方(在本例中为组织),或2)使用
build
方法,用于在呈现表单之前在用户中构建组织。我还找到了一个要点,描述了如果您愿意的话,将accepts_nested_attributes与belongs_to关系一起使用的潜在解决方案处理一些额外的代码。这也使用
build
方法。The doc epochwolf cited states in the first line "Nested attributes allow you to save attributes on associated records through the parent." (my emphasis).
You might be interested in this other SO question which is along the same lines as this one. It describes two possible solutions: 1) moving the accepts_nested_attributes to the other side of the relationship (in this case, Organization), or 2) using the
build
method to build the Organization in the User before rendering the form.I also found a gist that describes a potential solution for using accepts_nested_attributes with a belongs_to relationship if you're willing to deal with a little extra code. This uses the
build
method as well.对于 Rails 3.2 中的
belongs_to
关联,嵌套模型需要以下两个步骤:(1) 将新的
attr_accessible
添加到您的子模型(用户模型)。(2) 将
@user.build_organization
添加到您的子控制器(用户控制器)以创建列organization
。For
belongs_to
association in Rails 3.2, nested model needs the following two steps:(1) Add new
attr_accessible
to your child-model (User model).(2) Add
@user.build_organization
to your child-controller (User controller) in order to create columnorganization
.对于 Ruby on Rails 5.2.1
刚刚到达你的控制器,假设是“users_controller.rb”:
视图就像 Nick 所做的那样:
最后我们看到 @user3551164 已经解决了,但是现在(Ruby on Rails 5.2.1 )我们不需要
attr_accessible :organization_attributes
For Ruby on Rails 5.2.1
Just got to your controller, suppose to be "users_controller.rb":
And the view just as Nick did:
At end we see that @user3551164 have already solved, but now (Ruby on Rails 5.2.1) we don't need the
attr_accessible :organization_attributes