accepts_nested_attributes_for是否可以与belongs_to一起使用? [英] Does accepts_nested_attributes_for work with belongs_to?
问题描述
关于这个基本问题,我已经获得了各种各样相互矛盾的信息,而答案对于我当前的问题非常关键.那么,很简单,在Rails 3中,是否可以将带有accepts_to关系的accepts_nested_attributes_for一起使用?
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
在视图中:
= 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"
推荐答案
嵌套的属性对于从Rails 4开始的属植物关联似乎正常工作.在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.
这篇关于accepts_nested_attributes_for是否可以与belongs_to一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!