I populate the select collection (located in partial) based on the value in another collection set.
The dynamic collection selects the values, as expected, but when I submit the form, the value is missing (note that the rest of the data from all the other inputs of the form pass through).
Since the data from the dynamic collection selection is required, the controller redirects you back to the new view. Now, since the value of the first drop-down list has already been selected (since the last time you tried to fill out the form), the dynamic drop-down menu is already loaded with possible choices, and now the value of the dynamic selection is transmitted when the form is submitted. If you change the value of the first collection, select the second, it will not display its value until the second time (as before).
Does anyone know what I'm doing wrong?
Here is what I consider relevant parts of my code:
Partial containing dynamic collection set:
<%= collection_select(:f, :address_id, @addresses, :id, :concatenateAddress, {:include_blank => "Select an Address"}) %>
New and Create actions in my controller:
def new
@contact = Contact.new
@companies = ContactCompany.all
@addresses = []
respond_to do |format|
format.html
format.xml { render :xml => @contact }
end
end
def create
@contact = Contact.new(params[:contact])
respond_to do |format|
if @contact.save
flash[:notice] = 'Contact was successfully created.'
format.html { redirect_to(@contact) }
format.xml { render :xml => @contact, :status => :created, :location => @contact }
else
format.html { render :action => "new" }
format.xml { render :xml => @contact.errors, :status => :unprocessable_entity }
end
end
end
Details in my view (showing both collection selection cells and my observed field
<tr>
<td colspan = "2">
<%=
@company = ContactCompany.find(:all)
f.collection_select(:company_id, @company, :id, :name,{:selected => @contact.company_id })
%>
<br/>
<%= link_to 'Company Index', {:controller=>"contact_companies", :action=>"index"} %> | <%= link_to 'New Company', {:controller=>"contact_companies", :action=>"new"}%>
</td>
<td><%= f.text_field :phone_number %></td>
<td><%= f.text_field :fax_number %></td>
</tr>
<tr>
<td colspan = "2">Address</td>
<td>Cell Phone</td>
<td>Alternate Phone</td>
</tr>
<tr>
<td colspan = "2">
<% @addresses = ContactCompanyAddress.find(:all, :conditions=> {:company_id => @contact.company_id}) %>
<%= observe_field "contact_company_id", :url=>{:controller => 'contact_companies', :action=>'ajax_get_addresses', :id => ContactCompany.first}, :with=> 'company_id' %>
<div id="addresses"><%= render :partial => 'contact_companies/addresses', :object => @addresses, :f => 'f'%> </div>
<%= link_to 'Address Index', {:controller=>"contact_company_addresses", :action=>"index"} %> | <%= link_to 'New Address', {:controller=>"contact_company_addresses", :action=>"new"}%>
</td>
</tr>