Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allowing an object to be passed explicitly to a fields_for with neste…

…d_attributes on one-to-one associations

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1849 state:committed]
  • Loading branch information...
commit d15d53cf810014b90827015ecd0e601176492fb7 1 parent c96382a
Pascal Ehlert authored NZKoz committed
3  actionpack/lib/action_view/helpers/form_helper.rb
View
@@ -971,7 +971,8 @@ def fields_for_with_nested_attributes(association_name, args, block)
@template.fields_for(child_name, child, *args, &block)
end.join
else
- @template.fields_for(name, association, *args, &block)
+ object = args.first.respond_to?(:new_record?) ? args.first : association
+ @template.fields_for(name, object, *args, &block)
end
end
9 actionpack/test/template/form_helper_test.rb
View
@@ -586,6 +586,15 @@ def test_nested_fields_for_with_a_new_record_on_a_nested_attributes_one_to_one_a
assert_dom_equal expected, output_buffer
end
+ def test_nested_fields_for_with_explicitly_passed_object_on_a_nested_attributes_one_to_one_association
+ form_for(:post, @post) do |f|
+ f.fields_for(:author, Author.new(123)) do |af|
+ assert_not_nil af.object
+ assert_equal 123, af.object.id
+ end
+ end
+ end
+
def test_nested_fields_for_with_an_existing_record_on_a_nested_attributes_one_to_one_association
@post.author = Author.new(321)
Please sign in to comment.
Something went wrong with that request. Please try again.