title: set dynamic nested form name and ID attributes
body: <p>I have a  rails app with two models: Schedule and Task.  Task belongs_to Schedule and Schedule has_many Tasks.  I use a nested form with javascript to allow the user to enter as many tasks as they want when creating a schedule.  Here is my form:</p>
  
  <pre><code>&lt;%= form_for [@project, @schedule] do |f| %&gt;
      &lt;%= f.fields_for :tasks do |builder| %&gt;
          &lt;%= render 'task_fields', :f =&gt; builder %&gt;
      &lt;% end %&gt;
      &lt;p&gt;&lt;%= link_to_add_fields "Add task", f, :tasks %&gt;
      &lt;p&gt;&lt;%= f.submit "Submit" %&gt;&lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>And here is the partial the form above refers to:</p>
  
  <pre><code>&lt;p class = "fieldo"&gt;
  
       &lt;%= f.label :title %&gt;&lt;br /&gt;
       &lt;%= f.text_field :title %&gt;&lt;br /&gt;
  
       &lt;%= f.label :content %&gt;&lt;br /&gt;
       &lt;%= f.text_field :content %&gt;&lt;br /&gt;
  
       &lt;%= f.hidden_field :_destroy %&gt;
  
      &lt;%= link_to_function "remove", "remove_fields(this)"  %&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p>Here is the schedules controller action for the form:</p>
  
  <pre><code>def new
      2.times {@schedule.tasks.build}
  end
  </code></pre>
  
  <p>So, as you can see from the controller, the form will automatically print 2 task forms to the page.  Those 3 forms give the following html:</p>
  
  <pre><code>&lt;p class = "fieldo"&gt;
      &lt;input id="schedule_tasks_attributes_0_title" name="schedule[tasks_attributes][0][title]" type="text" /&gt;&lt;br /&gt;
      &lt;input id="schedule_tasks_attributes_0_content" name="schedule[tasks_attributes][0][content]" type="text" /&gt;&lt;br /&gt;
  &lt;/p&gt;
  &lt;p class = "fieldo"&gt;
      &lt;input id="schedule_tasks_attributes_1_title" name="schedule[tasks_attributes][1][title]" type="text" /&gt;&lt;br /&gt;
       &lt;input id="schedule_tasks_attributes_1_content" name="schedule[tasks_attributes][1][content]" type="text" /&gt;&lt;br /&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p>As you can see, the id and name start at 0 and increment by 1.  This is good and is how i would expect it to work.  The problem is that when i add a task dynamically by pressing the "Add task" button, the id and name become random numbers like this:</p>
  
  <pre><code>&lt;p class = "fieldo"&gt;
      &lt;input id="schedule_tasks_attributes_1387479041550_title" name="schedule[tasks_attributes][1387479041550][title]" type="text"&gt;
      &lt;input id="schedule_tasks_attributes_1387479041550_content" name="schedule[tasks_attributes][1387479041550][content]" type="text"&gt;
  &lt;/p&gt;
  &lt;p class = "fieldo"&gt;
      &lt;input id="schedule_tasks_attributes_1387479043642_title" name="schedule[tasks_attributes][1387479043642][title]" type="text"&gt;
      &lt;input id="schedule_tasks_attributes_1387479043642_content" name="schedule[tasks_attributes][1387479043642][content]" type="text"&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p>My questions are:</p>
  
  <ol>
  <li>What are those numbers and how are they being generated?</li>
  <li>Is there any way to change this to get neater numbers without breaking the functionality?</li>
  </ol>
  
  <p>If anyone has seen rails cast #197, this form is based slightly on that.  Here is my javascript and a ruby helper I am also using.  </p>
  
  <p>javascript:</p>
  
  <pre><code>//Dynamic forms
   function remove_fields(link) {
           $(link).prev("input[type=hidden]").val("1");
           $(link).closest(".fieldo").hide();
   }
  
   function add_fields(link, association, content) {
           var new_id = new Date().getTime();
           var regexp = new RegExp("new_" + association, "g");
           $(link).parent().before(content.replace(regexp, new_id)); 
  
   }
  </code></pre>
  
  <p>Application helper:</p>
  
  <pre><code>def link_to_add_fields(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_object, :child_index =&gt; "new_#{association}") do |builder|
          render(association.to_s.singularize + "_fields", :f =&gt; builder)
    end
    link_to_function(name, raw("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
  end
  </code></pre>