提交 a813fa09 编写于 作者: K Karol Galanciak

Accept lambda as child_index option in #fields_for method

上级 9040699a
* Accept lambda as `child_index` option in `fields_for` method.
*Karol Galanciak*
* `translate` allows `default: [[]]` again for a default value of `[]`.
Fixes #19640.
......
......@@ -1928,7 +1928,11 @@ def fields_for_with_nested_attributes(association_name, association, options, bl
explicit_child_index = options[:child_index]
output = ActiveSupport::SafeBuffer.new
association.each do |child|
options[:child_index] = nested_child_index(name) unless explicit_child_index
if explicit_child_index
options[:child_index] = explicit_child_index.call if explicit_child_index.respond_to?(:call)
else
options[:child_index] = nested_child_index(name)
end
output << fields_for_nested_model("#{name}[#{options[:child_index]}]", child, options, block)
end
output
......
......@@ -2878,6 +2878,23 @@ def test_nested_fields_for_with_child_index_option_override_on_a_nested_attribut
assert_dom_equal expected, output_buffer
end
def test_nested_fields_for_with_child_index_as_lambda_option_override_on_a_nested_attributes_collection_association
@post.comments = []
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
class FakeAssociationProxy
def to_ary
[1, 2, 3]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册