Permalink
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...
1 parent c96382a commit d15d53cf810014b90827015ecd0e601176492fb7 Pascal Ehlert committed with NZKoz Feb 2, 2009
@@ -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
@@ -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)

0 comments on commit d15d53c

Please sign in to comment.