<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -449,6 +449,15 @@ module ActionView
       #     &lt;% end %&gt;
       #   &lt;% end %&gt;
       #
+      # Or a collection to be used:
+      #
+      #   &lt;% form_for @person, :url =&gt; { :action =&gt; &quot;update&quot; } do |person_form| %&gt;
+      #     ...
+      #     &lt;% person_form.fields_for :projects, @active_projects do |project_fields| %&gt;
+      #       Name: &lt;%= project_fields.text_field :name %&gt;
+      #     &lt;% end %&gt;
+      #   &lt;% end %&gt;
+      #
       # When projects is already an association on Person you can use
       # +accepts_nested_attributes_for+ to define the writer method for you:
       #
@@ -1037,18 +1046,21 @@ module ActionView
 
         def fields_for_with_nested_attributes(association_name, args, block)
           name = &quot;#{object_name}[#{association_name}_attributes]&quot;
-          association = @object.send(association_name)
-          explicit_object = args.first.to_model if args.first.respond_to?(:to_model)
+          association = args.first.to_model if args.first.respond_to?(:to_model)
+
+          if association.respond_to?(:new_record?)
+            association = [association] if @object.send(association_name).is_a?(Array)
+          elsif !association.is_a?(Array)
+            association = @object.send(association_name)
+          end
 
           if association.is_a?(Array)
-            children = explicit_object ? [explicit_object] : association
             explicit_child_index = args.last[:child_index] if args.last.is_a?(Hash)
-
-            children.map do |child|
+            association.map do |child|
               fields_for_nested_model(&quot;#{name}[#{explicit_child_index || nested_child_index(name)}]&quot;, child, args, block)
             end.join
-          else
-            fields_for_nested_model(name, explicit_object || association, args, block)
+          elsif association
+            fields_for_nested_model(name, association, args, block)
           end
         end
 </diff>
      <filename>actionpack/lib/action_view/helpers/form_helper.rb</filename>
    </modified>
    <modified>
      <diff>@@ -784,6 +784,42 @@ class FormHelperTest &lt; ActionView::TestCase
     assert_dom_equal expected, output_buffer
   end
 
+  def test_nested_fields_for_with_an_empty_supplied_attributes_collection
+    form_for(:post, @post) do |f|
+      concat f.text_field(:title)
+      f.fields_for(:comments, []) do |cf|
+        concat cf.text_field(:name)
+      end
+    end
+
+    expected = '&lt;form action=&quot;http://www.example.com&quot; method=&quot;post&quot;&gt;' +
+               '&lt;input name=&quot;post[title]&quot; size=&quot;30&quot; type=&quot;text&quot; id=&quot;post_title&quot; value=&quot;Hello World&quot; /&gt;' +
+               '&lt;/form&gt;'
+
+    assert_dom_equal expected, output_buffer
+  end
+
+  def test_nested_fields_for_with_existing_records_on_a_supplied_nested_attributes_collection
+    @post.comments = Array.new(2) { |id| Comment.new(id + 1) }
+
+    form_for(:post, @post) do |f|
+      concat f.text_field(:title)
+      f.fields_for(:comments, @post.comments) do |cf|
+        concat cf.text_field(:name)
+      end
+    end
+
+    expected = '&lt;form action=&quot;http://www.example.com&quot; method=&quot;post&quot;&gt;' +
+               '&lt;input name=&quot;post[title]&quot; size=&quot;30&quot; type=&quot;text&quot; id=&quot;post_title&quot; value=&quot;Hello World&quot; /&gt;' +
+               '&lt;input id=&quot;post_comments_attributes_0_id&quot; name=&quot;post[comments_attributes][0][id]&quot; type=&quot;hidden&quot; value=&quot;1&quot; /&gt;' +
+               '&lt;input id=&quot;post_comments_attributes_0_name&quot; name=&quot;post[comments_attributes][0][name]&quot; size=&quot;30&quot; type=&quot;text&quot; value=&quot;comment #1&quot; /&gt;' +
+               '&lt;input id=&quot;post_comments_attributes_1_id&quot; name=&quot;post[comments_attributes][1][id]&quot; type=&quot;hidden&quot; value=&quot;2&quot; /&gt;' +
+               '&lt;input id=&quot;post_comments_attributes_1_name&quot; name=&quot;post[comments_attributes][1][name]&quot; size=&quot;30&quot; type=&quot;text&quot; value=&quot;comment #2&quot; /&gt;' +
+               '&lt;/form&gt;'
+
+    assert_dom_equal expected, output_buffer
+  end
+
   def test_nested_fields_for_on_a_nested_attributes_collection_association_yields_only_builder
     @post.comments = [Comment.new(321), Comment.new]
     yielded_comments = []</diff>
      <filename>actionpack/test/template/form_helper_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>a44a1257d879311d88c2d10c366ab0d6561f903a</id>
    </parent>
  </parents>
  <author>
    <name>Andrew France</name>
    <login>Odaeus</login>
    <email>andrew@avito.co.uk</email>
  </author>
  <url>http://github.com/rails/rails/commit/1b78e9bba3bd39c4669ff6c640b7df069185c22c</url>
  <id>1b78e9bba3bd39c4669ff6c640b7df069185c22c</id>
  <committed-date>2009-09-12T06:50:05-07:00</committed-date>
  <authored-date>2009-07-11T07:38:35-07:00</authored-date>
  <message>Allow fields_for on a nested_attributes association to accept an explicit collection to be used. [#2648 state:resolved]
Signed-off-by: Eloy Duran &lt;eloy.de.enige@gmail.com&gt;</message>
  <tree>58a1e6ea5b5c1e9e8714f1f1370c4369e669f004</tree>
  <committer>
    <name>Eloy Duran</name>
    <login>alloy</login>
    <email>eloy.de.enige@gmail.com</email>
  </committer>
</commit>
