Ruby on rails Rails嵌套表单在动态添加额外对象时不显示字段
我目前正在使用嵌套的表单gem,并且我正在尝试将多个房东添加到一个属性中 目前,这种联系相当深: 物业->房东->联系方式->地址 在我的属性控制器中,我正在建立关联,并且初始表单显示正确。但是,使用“添加字段”按钮后,没有字段。我知道这与物体没有被建造有关,但我不明白为什么 这是我的物业模型:Ruby on rails Rails嵌套表单在动态添加额外对象时不显示字段,ruby-on-rails,nested,nested-forms,nested-attributes,Ruby On Rails,Nested,Nested Forms,Nested Attributes,我目前正在使用嵌套的表单gem,并且我正在尝试将多个房东添加到一个属性中 目前,这种联系相当深: 物业->房东->联系方式->地址 在我的属性控制器中,我正在建立关联,并且初始表单显示正确。但是,使用“添加字段”按钮后,没有字段。我知道这与物体没有被建造有关,但我不明白为什么 这是我的物业模型: belongs_to :address belongs_to :estate_agent belongs_to :property_style has_and_belongs_to_many :lan
belongs_to :address
belongs_to :estate_agent
belongs_to :property_style
has_and_belongs_to_many :landlord
has_and_belongs_to_many :tenancy_agreement
attr_accessible :landlord_attributes, :address_attributes, :estate_agent_attributes,
:property_style_attributes, :sector, :reference , :occupied, :available_date, :property_style_attributes,...
accepts_nested_attributes_for :landlord, :address, :estate_agent, :property_style, :tenancy_agreement
这里是属性控制器中的新功能:
def new
@property = Property.new
@property.build_address
@property.landlord.build.build_contact_detail.build_address
@property.estate_agent_id = current_user.estate_agent_id
respond_to do |format|
format.html # new.html.erb
format.json { render json: @property }
end
end
@property.landlords.build.build_contact_detail.build_address
我在这方面做了很多尝试,但看不出哪里出了问题,嵌套形式的gem不支持这么多级别的关联或关联类型是不是有问题
谢谢
编辑
所作的修改:
belongs_to :address
belongs_to :estate_agent
belongs_to :property_style
has_and_belongs_to_many :landlords
has_and_belongs_to_many :tenancy_agreements
attr_accessible :landlords_attributes, :address_attributes, :estate_agent_attributes,
:property_style_attributes, :sector, :reference , :occupied, :available_date, :property_style_attributes,...
accepts_nested_attributes_for :landlords, :address, :estate_agent, :property_style, :tenancy_agreements
属性控制器:
def new
@property = Property.new
@property.build_address
@property.landlord.build.build_contact_detail.build_address
@property.estate_agent_id = current_user.estate_agent_id
respond_to do |format|
format.html # new.html.erb
format.json { render json: @property }
end
end
@property.landlords.build.build_contact_detail.build_address
房东模式
has_and_belongs_to_many :properties
以下是我的看法:
<%= nested_form_for(@property) do |f| %>
<% if @property.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@property.errors.count, "error") %> prohibited this property from being saved:</h2>
<ul>
<% @property.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<h2>Landlords</h2>
<%= f.fields_for :landlords %>
<p><%= f.link_to_add "Add a Landlord", :landlords %></p>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
禁止保存此属性:
地主
除非您指定“房东”为单数,否则Rails将假定它是单数。多对多协会应以复数形式宣布
尝试将多对多关联更改为:
has_and_belongs_to_many :landlords
has_and_belongs_to_many :tenancy_agreements
您还需要将所有对这些的调用更改为复数。此外,您必须将的
接受嵌套属性
更改为房东
,将属性可访问属性
从房东属性
更改为房东属性
我尝试使用可怕的嵌套形式和cocoon,但仍然无法使用
最后,我找到了一个解决方法,在部分而不是控制器中构建对象。像这样:
<% f.object.build_contact_detail.build_address %>
我希望这对其他人有帮助 哦,我觉得这是必须使用的标准命名惯例。我把它改成了复数形式,即使是在视图和控制器中(导致错误),但它仍然像以前一样工作。我注意到在我的房东模型中,它有
has\u和\u属于\u许多:属性
我是否也需要将这些更改为复数?谢谢我刚刚尝试将房东模型更新为:properties,但仍然存在相同的问题。使用habtm协会有问题吗?