I have the following models:
Entity has_many :locations
Location belongs_to :entity belongs_to :site
Site has_many :locations
I want to create a location and its related site in the locations/new.html.erb view.
I have this in the controller:
def create @entity = Entity.find params[:entity_id] @location = @entity.build_location(params[:location]) @site = @location.build_site
def update @entity = Entity.find params[:entity_id] @location = Location.find(params[:id]) @site = @location.site
The new form contains this:
<% form_for(@location) do |f| %>
<%= hidden_field @location, :entity_id hidden_field @location, :site_id -%>
<p> <b>Location type</b><br /> <%= f.select :location_type, [ ['POST - Postal or Main', 'POST'], ['DELV - Delivery', 'DELV'], ['SHIP - Shipping', 'SHIP'], ['OTHR - Other', 'OTHR'], ], :size => 4, :prompt => 'Primary use' -%> </p>
<p> <b>Location description</b><br /> <%= f.text_field :location_description, :size => 40 %> </p>
<!-- <p> <b>Site</b><br /> <%#= f.text_field :site_id, :size => 8 %> </p> -->
<%= render :partial => 'sites/site_detail', :object => @location.site -%>
...
The problem is in the parameters passed back on the create. What is being sent back to the locations_controller is:
{"#<Location:0x4953d10>"=>{"site_id"=>""}, "commit"=>"Create", "authenticity_token"=>"547f4ee57048fb38fc253ce809bb7c7e4405b546", "location"=>{"location_description"=>" test", "location_type"=>"POST"}, "nil_class"=>{"site_postal_code"=>"A1A Z9Z", "site_municipality"=>"Toronto", "site_street_number"=>"9", "site_region"=>"Ontario", "site_name"=>"test site", "site_country_code"=>"CA", "site_building_floor"=>"1", "site_street_name"=>"test", "site_building_name"=>"Test, Test and Test Building", "site_building_unit_number"=>""}}
So, I have a nil class where I expect @location.site and entity_id is completely absent. I am evidently wrecking something in the way I have set up my form and I would great appreciate it if anyone could tell me what I am doing wrong and how to fix it.