I'm currently using the nested_forms gem and I'm trying to be able to add multiple landlords to a property.
At the moment the associations are quite deep: Property -> Landlord -> Contact_Detail -> Address
In my Property controller I'm building the associations and the initial form is displayed correctly. However, after using the add fields button, there are no fields. I know it is something to do with the object not getting built, but I can't understand why.
Here's my Property model:
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
And here's the new function in the Property controller:
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
I've had quite a few attempts at this, but can't see where I'm going wrong, is it a problem with the nested_form gem not supporting this many levels of association or the type of association?
Thanks!
EDIT Changes made:
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
Properties controller:
@property.landlords.build.build_contact_detail.build_address
Landlords model
has_and_belongs_to_many :properties
Here is my view:
<%= 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 %>