<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>app/concerns/nested_params_form_builder.rb</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -1,3 +1,5 @@
+load 'nested_params_form_builder.rb'
+
 class ProjectsController &lt; ApplicationController
   def index
     @projects = Project.find(:all)
@@ -24,10 +26,11 @@ class ProjectsController &lt; ApplicationController
   
   def edit
     @project = Project.find(params[:id])
+    # add an extra new record for debugging purposes
+    @project.tasks.build
   end
   
   def update
-    #params[:project][:tasks] ||= []
     @project = Project.find(params[:id])
     if @project.update_attributes(params[:project])
       flash[:notice] = &quot;Successfully updated project.&quot;</diff>
      <filename>app/controllers/projects_controller.rb</filename>
    </modified>
    <modified>
      <diff>@@ -2,16 +2,18 @@
 
 &lt;%= error_messages_for :project %&gt;
 
-&lt;% form_for @project do |f| -%&gt;
+&lt;% form_for @project, :builder =&gt; NestedParamsFormBuilder do |f| -%&gt;
+
   &lt;p&gt;
     &lt;%= f.label :name, &quot;Project:&quot; %&gt;
     &lt;%= f.text_field :name %&gt;
   &lt;/p&gt;
   &lt;div id=&quot;tasks&quot;&gt;
-    &lt;%= render :partial =&gt; 'task', :collection =&gt; @project.tasks %&gt;
+    &lt;%= render :partial =&gt; 'task', :collection =&gt; @project.tasks, :locals =&gt; { :pf =&gt; f } %&gt;
+    &lt;/div&gt;
   &lt;/div&gt;
   &lt;p&gt;
-    &lt;%= add_task_link &quot;Add a task&quot; %&gt;
+    &lt;%#= add_task_link &quot;Add a task&quot; %&gt;
   &lt;/p&gt;
   &lt;p&gt;
     &lt;%= f.submit &quot;Submit&quot; %&gt;</diff>
      <filename>app/views/projects/_form.html.erb</filename>
    </modified>
    <modified>
      <diff>@@ -1,11 +1,9 @@
 &lt;div class=&quot;task&quot;&gt;
-&lt;% fields_for 'project[tasks]', task, :index =&gt; '' do |f| -%&gt;
-  &lt;%= error_messages_for :task, :object =&gt; task %&gt;
-  &lt;%= f.hidden_field :id unless task.new_record? %&gt;
+&lt;% pf.fields_for :tasks, task do |f| %&gt;
+  &lt;%#= error_messages_for :task, :object =&gt; task %&gt;
   &lt;p&gt;
     &lt;%= f.label :name, &quot;Task:&quot; %&gt;
     &lt;%= f.text_field :name %&gt;
     &lt;%= link_to_function &quot;remove&quot;, &quot;$(this).up('.task').remove()&quot; %&gt;
   &lt;/p&gt;
-&lt;% end -%&gt;
-&lt;/div&gt;
+&lt;% end %&gt;
\ No newline at end of file</diff>
      <filename>app/views/projects/_task.html.erb</filename>
    </modified>
    <modified>
      <diff>@@ -32,7 +32,7 @@ Rails::Initializer.run do |config|
   # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
 
   # Add additional load paths for your own custom dirs
-  # config.load_paths += %W( #{RAILS_ROOT}/extras )
+  config.load_paths += %W( #{RAILS_ROOT}/app/concerns )
 
   # Force all environments to use the same logger level
   # (by default production uses :info, the others :debug)</diff>
      <filename>config/environment.rb</filename>
    </modified>
    <modified>
      <diff>@@ -49,6 +49,9 @@ body {
 .fieldWithErrors {
   display: inline;
 }
+.fieldWithErrors input {
+  border: 1px solid red;
+}
 
 #errorExplanation {
   width: 400px;</diff>
      <filename>public/stylesheets/application.css</filename>
    </modified>
    <modified>
      <diff>@@ -50,13 +50,18 @@ module NestedParams
     class_eval do
       define_method(&quot;#{attr}_with_nested_params=&quot;) do |value|
         if value.is_a? Hash
-          # For existing records
+          # For existing records and new records that are marked by an id that starts with 'new_'
           value.each do |id, attributes|
-            send(attr).detect { |x| x.id == id.to_i }.attributes = attributes
+            if id.starts_with? 'new_'
+              send(attr).build attributes
+            else
+              # Find the record for this id and assign the attributes
+              send(attr).detect { |x| x.id == id.to_i }.attributes = attributes
+            end
           end
         else
           if value.is_a?(Array) &amp;&amp; value.all? { |x| x.is_a?(Hash) }
-            # For new records
+            # For an array full of new record hashes
             value.each do |attributes|
               send(attr).build attributes
             end</diff>
      <filename>vendor/plugins/has_autosave_and_nested_params/lib/nested_params.rb</filename>
    </modified>
    <modified>
      <diff>@@ -63,6 +63,22 @@ describe &quot;NestedParams, on a has_many association&quot; do
     @visitor.reload
     @visitor.artists.map(&amp;:name).sort.should == %w{ jack joe }
   end
+  
+  it &quot;should update existing records and add new ones that have an id that start with the string 'new_'&quot; do
+    before = Artist.count
+    
+    @visitor.update_attributes({
+      :artists =&gt; {
+        @artist1.id.to_s =&gt; { :name =&gt; 'joe' },
+        &quot;new_12345&quot; =&gt; { :name =&gt; 'jill' },
+        @artist2.id.to_s =&gt; { :name =&gt; 'jack' }
+      }
+    })
+    @visitor.reload
+    
+    Artist.count.should.be before + 1
+    @visitor.artists.map(&amp;:name).sort.should == %w{ jack jill joe }
+  end
 end
 
 describe &quot;NestedParams, on a has_one association&quot; do</diff>
      <filename>vendor/plugins/has_autosave_and_nested_params/test/nested_params_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>c1cd32ba8ff8e38e426b72f36db59f3b46994e0c</id>
    </parent>
  </parents>
  <author>
    <name>Eloy Duran</name>
    <email>eloy.de.enige@gmail.com</email>
  </author>
  <url>http://github.com/alloy/complex-form-examples/commit/9f711f2ae148eca7b95864f1c537c99859d2b051</url>
  <id>9f711f2ae148eca7b95864f1c537c99859d2b051</id>
  <committed-date>2008-08-19T16:57:03-07:00</committed-date>
  <authored-date>2008-08-19T16:57:03-07:00</authored-date>
  <message>Toying with fields_for a has_many association and finding a way to have NestedParams build new records from those generated forms.

In the case of a new record we simply generate a fictive id, but since we don't need it afterwards there's no point in keeping
a counter around, thus we simply generate an id like 'new_12345' where the number is the #object_id of the record.
We can then catch these new records in the NestedParams writer method and build new records.</message>
  <tree>727a599af8436805acf495eb32d459e7828dda5b</tree>
  <committer>
    <name>Eloy Duran</name>
    <email>eloy.de.enige@gmail.com</email>
  </committer>
</commit>
