Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #8623 from virusman/form_helpers_collectionproxy_fix

Fixed nested fields bug when called with AssociationProxy
  • Loading branch information...
commit 94797ed146ba02f627a365293e46b6e8c42a4fd7 2 parents 15b7482 + c009e86
@jonleighton jonleighton authored
View
2  actionpack/lib/action_view/helpers/form_helper.rb
@@ -1800,7 +1800,7 @@ def fields_for_with_nested_attributes(association_name, association, options, bl
association = convert_to_model(association)
if association.respond_to?(:persisted?)
- association = [association] if @object.send(association_name).is_a?(Array)
+ association = [association] if @object.send(association_name).respond_to?(:to_ary)
elsif !association.respond_to?(:to_ary)
association = @object.send(association_name)
end
View
23 actionpack/test/template/form_helper_test.rb
@@ -2163,6 +2163,29 @@ def test_nested_fields_for_with_child_index_option_override_on_a_nested_attribut
assert_dom_equal expected, output_buffer
end
+ class FakeAssociatonProxy
+ def to_ary
+ [1, 2, 3]
+ end
+ end
+
+ def test_nested_fields_for_with_child_index_option_override_on_a_nested_attributes_collection_association_with_proxy
+ @post.comments = FakeAssociatonProxy.new
+
+ form_for(@post) do |f|
+ concat f.fields_for(:comments, Comment.new(321), :child_index => 'abc') { |cf|
+ concat cf.text_field(:name)
+ }
+ end
+
+ expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do
+ '<input id="post_comments_attributes_abc_name" name="post[comments_attributes][abc][name]" type="text" value="comment #321" />' +
+ '<input id="post_comments_attributes_abc_id" name="post[comments_attributes][abc][id]" type="hidden" value="321" />'
+ end
+
+ assert_dom_equal expected, output_buffer
+ end
+
def test_nested_fields_for_index_method_with_existing_records_on_a_nested_attributes_collection_association
@post.comments = Array.new(2) { |id| Comment.new(id + 1) }
Please sign in to comment.
Something went wrong with that request. Please try again.