Skip to content
This repository
Browse code

Allow fields_for on a nested_attributes association to accept an expl…

…icit collection to be used. [#2648 state:resolved]

Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
  • Loading branch information...
commit 1b78e9bba3bd39c4669ff6c640b7df069185c22c 1 parent a44a125
Andrew France authored July 11, 2009 alloy committed September 12, 2009
26  actionpack/lib/action_view/helpers/form_helper.rb
@@ -449,6 +449,15 @@ def apply_form_for_options!(object_or_array, options) #:nodoc:
449 449
       #     <% end %>
450 450
       #   <% end %>
451 451
       #
  452
+      # Or a collection to be used:
  453
+      #
  454
+      #   <% form_for @person, :url => { :action => "update" } do |person_form| %>
  455
+      #     ...
  456
+      #     <% person_form.fields_for :projects, @active_projects do |project_fields| %>
  457
+      #       Name: <%= project_fields.text_field :name %>
  458
+      #     <% end %>
  459
+      #   <% end %>
  460
+      #
452 461
       # When projects is already an association on Person you can use
453 462
       # +accepts_nested_attributes_for+ to define the writer method for you:
454 463
       #
@@ -1037,18 +1046,21 @@ def nested_attributes_association?(association_name)
1037 1046
 
1038 1047
         def fields_for_with_nested_attributes(association_name, args, block)
1039 1048
           name = "#{object_name}[#{association_name}_attributes]"
1040  
-          association = @object.send(association_name)
1041  
-          explicit_object = args.first.to_model if args.first.respond_to?(:to_model)
  1049
+          association = args.first.to_model if args.first.respond_to?(:to_model)
  1050
+
  1051
+          if association.respond_to?(:new_record?)
  1052
+            association = [association] if @object.send(association_name).is_a?(Array)
  1053
+          elsif !association.is_a?(Array)
  1054
+            association = @object.send(association_name)
  1055
+          end
1042 1056
 
1043 1057
           if association.is_a?(Array)
1044  
-            children = explicit_object ? [explicit_object] : association
1045 1058
             explicit_child_index = args.last[:child_index] if args.last.is_a?(Hash)
1046  
-
1047  
-            children.map do |child|
  1059
+            association.map do |child|
1048 1060
               fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index(name)}]", child, args, block)
1049 1061
             end.join
1050  
-          else
1051  
-            fields_for_nested_model(name, explicit_object || association, args, block)
  1062
+          elsif association
  1063
+            fields_for_nested_model(name, association, args, block)
1052 1064
           end
1053 1065
         end
1054 1066
 
36  actionpack/test/template/form_helper_test.rb
@@ -784,6 +784,42 @@ def test_nested_fields_for_with_existing_and_new_records_on_a_nested_attributes_
784 784
     assert_dom_equal expected, output_buffer
785 785
   end
786 786
 
  787
+  def test_nested_fields_for_with_an_empty_supplied_attributes_collection
  788
+    form_for(:post, @post) do |f|
  789
+      concat f.text_field(:title)
  790
+      f.fields_for(:comments, []) do |cf|
  791
+        concat cf.text_field(:name)
  792
+      end
  793
+    end
  794
+
  795
+    expected = '<form action="http://www.example.com" method="post">' +
  796
+               '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
  797
+               '</form>'
  798
+
  799
+    assert_dom_equal expected, output_buffer
  800
+  end
  801
+
  802
+  def test_nested_fields_for_with_existing_records_on_a_supplied_nested_attributes_collection
  803
+    @post.comments = Array.new(2) { |id| Comment.new(id + 1) }
  804
+
  805
+    form_for(:post, @post) do |f|
  806
+      concat f.text_field(:title)
  807
+      f.fields_for(:comments, @post.comments) do |cf|
  808
+        concat cf.text_field(:name)
  809
+      end
  810
+    end
  811
+
  812
+    expected = '<form action="http://www.example.com" method="post">' +
  813
+               '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
  814
+               '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' +
  815
+               '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' +
  816
+               '<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />' +
  817
+               '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' +
  818
+               '</form>'
  819
+
  820
+    assert_dom_equal expected, output_buffer
  821
+  end
  822
+
787 823
   def test_nested_fields_for_on_a_nested_attributes_collection_association_yields_only_builder
788 824
     @post.comments = [Comment.new(321), Comment.new]
789 825
     yielded_comments = []

0 notes on commit 1b78e9b

Please sign in to comment.
Something went wrong with that request. Please try again.