From 04f84470132f5c326d8e9a59b4047ae4076bcc96 Mon Sep 17 00:00:00 2001 From: David Yip Date: Sun, 8 Mar 2009 22:27:57 -0500 Subject: [PATCH] Don't yield a form builder for nil one-to-one associations. Also added documentation on behavior of one-to-one association builders on associations that evaluate to nil. --- actionpack/lib/action_view/helpers/form_helper.rb | 20 +++++++++++++++++++- actionpack/test/template/form_helper_test.rb | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index a589bcb..0c54f6c 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -403,6 +403,19 @@ module ActionView # <% end %> # <% end %> # + # Form builders on models obtained from one-to-one associations only + # execute when the underlying model objects actually exist. In this + # example, if +address+ were nil, then the address builder would have + # yielded no output. To ensure that your forms are rendered, you must + # make sure that your model objects exist when appropriate: + # + # class Person + # def initialize + # @address = Address.new + # end + # ... + # end + # # ==== One-to-many # # Consider a Person class which returns an _array_ of Project instances @@ -1010,7 +1023,12 @@ module ActionView fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index}]", child, args, block) end.join else - fields_for_nested_model(name, explicit_object || association, args, block) + object = explicit_object || association + if object + fields_for_nested_model(name, object, args, block) + else + "" + end end end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 654eee4..b39cec1 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -473,6 +473,23 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_nested_fields_for_does_not_yield_builder_on_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