title: Need help understanding how to add and/remove objects within nested form
body: <p>I am having trouble adding the data from a nested model to my database. </p>
  
  <p>I have a Photo model with the following relationship to a Monster model. I am able to add and remove my nested form for monsters, but can't figure out how to actually add a new Monster object when updating my Photo. This is based on 1 nested level form from the <a href="http://railscasts.com/episodes/196-nested-model-form-revised" rel="nofollow">http://railscasts.com/episodes/196-nested-model-form-revised</a> example, basically the Survey - Question relationship demonstrated. </p>
  
  <pre><code>class Photo &lt; ActiveRecord::Base
    attr_accessible :title, :monsters_attributes
  
    has_and_belongs_to_many :monsters
  
    accepts_nested_attributes_for :monsters, allow_destroy: true
  end
  
  class Monster &lt; ActiveRecord::Base
    attr_accessible :first_name, :last_name, :selected
  
    has_and_belongs_to_many :photos
  end
  </code></pre>
  
  <p>I'm sure the trouble is in my photos_controller and my poor understanding of how to use the params submitted when I update my photo form. I get the following params hash when submitting an edit photo form with two monsters. The first one is already associated and the second one I am trying to declare by clicking an "Add Monster" link, entering the first and last name, and submitting the form.</p>
  
  <p>The "Add Monster" link is defined in my _form.html.erb file for a Photo and I am using a partial to render the fields. The helper function is declared in application_helper.rb.</p>
  
  <h3> _form.html.erb </h3>
  
  <pre><code>…
      &lt;!-- this uses the _monster_fields partial --&gt;
      &lt;%= f.fields_for :monsters do |builder| %&gt;
        &lt;%= render 'monster_fields', f: builder %&gt;
      &lt;% end %&gt;        
  
    &lt;/table&gt;
  
    &lt;%= link_to_add_fields "Add Monster", f, :monsters %&gt;
  ...
  
  application_helper.rb
  
  module ApplicationHelper
  
    def link_to_add_fields(name, f, association)
      new_object = f.object.send(association).klass.new  # make instance of monster association record, Monster.new
      id = new_object.object_id                          # get id of new monster object, id = Monster.new.object_id
  
      fields = f.fields_for(association, new_object, child_index: id) do |builder|
        render(association.to_s.singularize + "_fields", f: builder)    # render partial _monster_fields.html.erb
      end
  
      link_to(name, "#", class: "add_fields", data: {id: id, fields: fields.gsub("\n", '&amp;#xA')}) # returns a link    
    end
  end
  </code></pre>
  
  <h3>params shown from edit (update) of photo to show:</h3>
  
  <pre><code>{
  "utf8"=&gt;"✓", 
  "_method"=&gt;"put", 
  "authenticity_token"=&gt;"8dYkFrzdkulBv8YrZCHU2wfFh4v5LQc9q/JWPmsbDkc=", 
  "photo"=&gt;{"title"=&gt;"Family Gathering", 
      "monsters_attributes"=&gt;{
      "0"=&gt;{"first_name"=&gt;"Franken", "last_name"=&gt;"Stein", "_destroy"=&gt;"false", "id"=&gt;"1"}, 
      "1358291763102"=&gt;{"first_name"=&gt;"Ware", "last_name"=&gt;"Wolf", "_destroy"=&gt;"false"}
      }, 
      "monster_ids"=&gt;"1"
     }, 
  "commit"=&gt;"Update Photo", 
  "action"=&gt;"update", 
  "controller"=&gt;"photos", 
  "id"=&gt;"1"
  }
  </code></pre>
  
  <p>In my applcation.js file I define how to handle the adding and removal of fields. In .add_fields, the example uses the id for my newly created object but I don't see how to save my newly created monster in the monsters table.</p>
  
  <h3>application.js</h3>
  
  <pre><code>$(document).ready(function() {
  
    $('form').on('click', '.remove_fields', function(event) {
      $(this).prev('input[type=hidden]').val('1');
      $(this).closest('tr').hide();
      event.preventDefault();
    });
  
    $('form').on('click', '.add_fields', function(event) {
      time = new Date().getTime();
      regexp = new RegExp($(this).data('id'), 'g');
      $(this).before($(this).data('fields').replace(regexp, time))    
      event.preventDefault();
    });
  
  });
  </code></pre>
  
  <p>Overall, it's a pretty simple process but I'm not sure how to proceed further when it comes to actually saving a new Monster from my Photo edit view(creating a new active record object). </p>
  
  <p>Could I have issues using a many-to-many relationship?</p>
  
  <p>The example from railscasts is good but it doesn't quite go that far. I am grateful for any assistance someone may have. </p>
  
  <p><a href="http://railscasts.com/episodes/196-nested-model-form-revised" rel="nofollow">http://railscasts.com/episodes/196-nested-model-form-revised</a></p>
  
  <h2>updated 1/16/13: My params hash and the error I get</h2>
  
  <p>ActiveModel::MassAssignmentSecurity::Error in PhotosController#update</p>
  
  <p>Can't mass-assign protected attributes: monster_ids</p>
  
  <p>app/controllers/photos_controller.rb:76:in <code>block in update'
  app/controllers/photos_controller.rb:75:in</code>update'</p>
  
  <pre><code>{"utf8"=&gt;"✓",
   "_method"=&gt;"put",
   "authenticity_token"=&gt;"1l62Sged52hnEHQoj0WPf/asRjcIpIKBBzsb7gVDRm0=",
   "photo"=&gt;{"title"=&gt;"Family Gathering",
   "monsters_attributes"=&gt;{"0"=&gt;{"first_name"=&gt;"Franken",
   "last_name"=&gt;"Stein",
   "_destroy"=&gt;"false",
   "id"=&gt;"1"},
   "1358356969634"=&gt;{"first_name"=&gt;"Scary",
   "last_name"=&gt;"Frank",
   "_destroy"=&gt;"false"}},
   "monster_ids"=&gt;"1"},
   "commit"=&gt;"Update Photo",
   "id"=&gt;"1"}
  </code></pre>