I'm trying to make a form to set values to two children objects through has_one (with :class_name option) and belongs_to relation. However, when I input and submit values through the form, both children objects have the same value even if I input different values.
I have the these two models. (two children objects above indicates "origin" and "destination" whose class names are "Place")
class Route < ActiveRecord::Base
attr_accessible :name, :destination_attributes, :origin_attributes
has_one :origin, :class_name=>"Place"
has_one :destination, :class_name=>"Place"
accepts_nested_attributes_for :origin, :destination
end
class Place < ActiveRecord::Base
attr_accessible :address, :lat, :lng, :name, :route_id
belongs_to :route, :foreign_key => "route_id"
end
And made form using partial like following.
routes/_form.html.erb
<%= form_for(@route) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
<br />
<%= render :partial => "places/nested_places_form", :locals => {record_name: :origin, place_object: @route.origin, parent_form: f} %>
<br />
<%= render :partial => "places/nested_places_form", :locals => {record_name: :destination, place_object: @route.destination, parent_form: f} %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
places/nested_places_form.html.erb
<%= parent_form.fields_for record_name, place_object do |t| %>
<%= record_name %>
<% if place_object.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@place.errors.count, "error") %> prohibited this place from being saved:</h2>
<ul>
<% @place.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= t.label :name %><br />
<%= t.text_field :name %>
</div>
<div class="field">
<%= t.label :lat %><br />
<%= t.text_field :lat %>
</div>
<div class="field">
<%= t.label :lng %><br />
<%= t.text_field :lng %>
</div>
<% end %>
Like I mentioned, the attributes of origin and destination always end up the same even when I put different values in blanks and submit from form.
How can I make this work?