From 6875fe118c251d435021b5e77a036816b6cf873b Mon Sep 17 00:00:00 2001 From: David Yip Date: Sun, 8 Mar 2009 05:35:31 -0500 Subject: [PATCH] Render a form with all empty values if a nested association evaluates to nil. --- actionpack/lib/action_view/helpers/form_helper.rb | 2 ++ actionpack/test/template/form_helper_test.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 0 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index a589bcb..d4cd4b6 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -1009,6 +1009,8 @@ module ActionView children.map do |child| fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index}]", child, args, block) end.join + elsif association.nil? + @template.fields_for(name, *args, &block) else fields_for_nested_model(name, explicit_object || association, args, block) end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 654eee4..fc14acf 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -473,6 +473,24 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_nested_fields_for_with_nil_association + @post.author = nil + + form_for('post', @post) do |f| + concat f.text_field(:title) + f.fields_for :author do |c| + concat c.text_field(:name) + end + end + + expected = '
' + + '' + + '' + + '
' + + assert_dom_equal expected, output_buffer + end + def test_nested_fields_for_with_index_and_parent_fields form_for('post', @post, :index => 1) do |c| concat c.text_field(:title) -- 1.6.1