title: Undefined method "id" trying to save nested attributes in the same form
body: <p>I am developing a web application with Rails in which I need to save two models with the same form. One of the models (Characteristic) belongs to the other (Facilities), so I decided to use a accepts_nested_attributes_for for the contained model. In the view, I use form_for to save the parent model (Characteristic) and another form_for for the contained model (Facilities). However, I always obtain the same error:</p>
  
  <pre><code>Started PUT "/facilities/537f8adfb4f2d7c124000056" for 127.0.0.1 at 2014-05-31 20:00:23 +0200
  Processing by FacilitiesController#update as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"xr+cGlb9onx4o13IaS3K5UfYzmrb6pMdKljBc8byKdY=", "facilities"=&gt;{"description"=&gt;"Services", "characteristics"=&gt;[{"id"=&gt;"537f8adfb4f2d7c124000057", "title"=&gt;"Room", "description"=&gt;"Free"}]}, "commit"=&gt;"Send", "id"=&gt;"537f8adfb4f2d7c124000056"}
    MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=&gt;1} (1.2872ms)
    MOPED: 127.0.0.1:27017 QUERY        database=hotel_abadi_development collection=facilities selector={"_id"=&gt;"537f8adfb4f2d7c124000056"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.4916ms)
    MOPED: 127.0.0.1:27017 QUERY        database=hotel_abadi_development collection=admins selector={"$query"=&gt;{"_id"=&gt;"537f8ad9b4f2d7c124000001"}, "$orderby"=&gt;{:_id=&gt;1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.7987ms)
    MOPED: 127.0.0.1:27017 QUERY        database=hotel_abadi_development collection=admins selector={"$query"=&gt;{"_id"=&gt;"537f8ad9b4f2d7c124000001"}, "$orderby"=&gt;{:_id=&gt;1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.7885ms)
    MOPED: 127.0.0.1:27017 QUERY        database=hotel_abadi_development collection=facilities selector={"$query"=&gt;{"admin_id"=&gt;"537f8ad9b4f2d7c124000001"}, "$orderby"=&gt;{:_id=&gt;1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.8206ms)
  Completed 500 Internal Server Error in 502.0ms
  
  NoMethodError (undefined method `id' for #&lt;ActiveSupport::HashWithIndifferentAccess:0xa6ae334&gt;):
    app/controllers/facilities_controller.rb:21:in `update'
  
  
    Rendered /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-3.2.15/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.5ms)
    Rendered /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-3.2.15/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.4ms)
    Rendered /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-3.2.15/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (17.7ms)
  </code></pre>
  
  <p>In order to solve the problem, I have also tried to use fields_for with the nested attributes but I obtain the same error. The main files which defines the application are the next:</p>
  
  <p><strong>app/models/facilities.rb</strong></p>
  
  <pre><code>class Facilities
    ...
    field :description, type: String
    field :language,    type: Symbol, default: :es
  
    has_many   :characteristics, dependent: :destroy
  
    accepts_nested_attributes_for :characteristics, allow_destroy: true
    ...
  end
  </code></pre>
  
  <p><strong>app/models/characteristic.rb</strong></p>
  
  <pre><code>class Characteristic
    ...
    field :title,       type: String
    field :description, type: String
    field :language,    type: Symbol, default: :es
  
    belongs_to :admin
    has_one    :upload, dependent: :destroy
  
    accepts_nested_attributes_for :upload, allow_destroy: true
    ...
  end
  </code></pre>
  
  <p><strong>app/controllers/facilities_controller.rb</strong></p>
  
  <pre><code>class FacilitiesController &lt; ApplicationController
    load_and_authorize_resource
    respond_to :json, :html
    ...
    def update
      @facilities.update_attributes!( params[:facilities] )
      respond_with @facilities, api_template: :general, location: hotel_path
    end
    ...
  end
  </code></pre>
  
  <p><strong>app/views/facilities.html.haml</strong></p>
  
  <pre><code>= form_for facilities, url: facilities_path( facilities ) do |f|
    = f.text_area :description
    .facilities_form
      - facilities.characteristics.each_with_index do |char, index|
        = form_for characteristic, url: characteristic_path( characteristic ), html: { method: :put } do |d|
          = d.hidden_field :id, name: 'facilities[characteristics][][id]'
          = d.text_field :title, width: 20, size: 20, name: 'facilities[characteristics][][title]'
          = d.text_area :description, width: 20, rows: 4, cols: 22, name: 'facilities[characteristics][][description]'
  
    = f.submit "Send"
  </code></pre>
  
  <hr>
  
  <p><strong>Solved:</strong></p>
  
  <p>In the nested attributes, I manually put the name of the fields because I am using another form_for for them. In that names, I use "facilities[<strong>characteristics</strong>][][name_of_field]", but when we need to use nested attributes, we have to put "characteristics_attributes", so the correct name is "facilities[<strong>characteristics_attributes</strong>][][name_of_field]".</p>