aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPascal Ehlert <dadark@pascal-ehlerts-macbook-pro.local>2009-02-02 22:49:28 +0100
committerMichael Koziarski <michael@koziarski.com>2009-02-06 13:26:58 +1300
commitd15d53cf810014b90827015ecd0e601176492fb7 (patch)
tree17360de26c0d809055db59953ee38199bbc348df
parentc96382a71a77505ea2f51e43bc81fdb3ac95d117 (diff)
downloadrails-d15d53cf810014b90827015ecd0e601176492fb7.tar.gz
rails-d15d53cf810014b90827015ecd0e601176492fb7.tar.bz2
rails-d15d53cf810014b90827015ecd0e601176492fb7.zip
Allowing an object to be passed explicitly to a fields_for with nested_attributes on one-to-one associations
Signed-off-by: Michael Koziarski <michael@koziarski.com> [#1849 state:committed]
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb3
-rw-r--r--actionpack/test/template/form_helper_test.rb9
2 files changed, 11 insertions, 1 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 2ac2427884..0651f75cfb 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -971,7 +971,8 @@ module ActionView
@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
diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb
index 33a542af7e..b7ea2c0176 100644
--- a/actionpack/test/template/form_helper_test.rb
+++ b/actionpack/test/template/form_helper_test.rb
@@ -586,6 +586,15 @@ class FormHelperTest < ActionView::TestCase
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)