Permalink
Browse files

Added the :child_index option for a nested fields_for which uses that…

… index instead of generating one with #nested_child_index. This is handy for, for instance, when you are rendering html for use with JS and want to easily replace the index with a custom index on the client side.
  • Loading branch information...
1 parent 3d26bb0 commit debc14054aa2b2c7fd1d1494e299352dd02a8314 @alloy committed Feb 7, 2009
Showing with 20 additions and 1 deletion.
  1. +3 −1 actionpack/lib/action_view/helpers/form_helper.rb
  2. +17 −0 actionpack/test/template/form_helper_test.rb
@@ -966,8 +966,10 @@ def fields_for_with_nested_attributes(association_name, args, block)
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|
- fields_for_nested_model("#{name}[#{nested_child_index}]", child, args, block)
+ fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index}]", child, args, block)
end.join
else
fields_for_nested_model(name, explicit_object || association, args, block)
@@ -703,6 +703,23 @@ def test_nested_fields_for_on_a_nested_attributes_collection_association_yields_
assert_equal yielded_comments, @post.comments
end
+ def test_nested_fields_for_with_child_index_option_override_on_a_nested_attributes_collection_association
+ @post.comments = []
+
+ form_for(:post, @post) do |f|
+ f.fields_for(:comments, Comment.new(321), :child_index => 'abc') do |cf|
+ concat cf.text_field(:name)
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input id="post_comments_attributes_abc_id" name="post[comments_attributes][abc][id]" type="hidden" value="321" />' +
+ '<input id="post_comments_attributes_abc_name" name="post[comments_attributes][abc][name]" size="30" type="text" value="comment #321" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ end
+
def test_fields_for
fields_for(:post, @post) do |f|
concat f.text_field(:title)

0 comments on commit debc140

Please sign in to comment.