diff options
author | Jamis Buck <jamis@37signals.com> | 2007-01-17 00:04:02 +0000 |
---|---|---|
committer | Jamis Buck <jamis@37signals.com> | 2007-01-17 00:04:02 +0000 |
commit | b0a1aa7e7e4bc1b0a714fd143cc91944e2f4d230 (patch) | |
tree | 99839f7b128dfed242bf2a6267adc8b8b70d4fad | |
parent | 932e7b003ce77d9265e910cff63a17589b8c02a2 (diff) | |
download | rails-b0a1aa7e7e4bc1b0a714fd143cc91944e2f4d230.tar.gz rails-b0a1aa7e7e4bc1b0a714fd143cc91944e2f4d230.tar.bz2 rails-b0a1aa7e7e4bc1b0a714fd143cc91944e2f4d230.zip |
Allow fields_for to be nested in form_for
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5965 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 17 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 33 |
3 files changed, 45 insertions, 7 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 5720d9713f..c3cfcc0ab6 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Allow fields_for to be nested inside form_for, so that the name and id get properly constructed [Jamis Buck] + * Allow inGroupsOf and eachSlice to be called through rjs. #7046 [Cody Fauser] * Allow exempt_from_layout :rhtml. #6742, #7026 [dcmanges, Squeegy] diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 8aa0b42fe1..1c0829b300 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -401,11 +401,15 @@ module ActionView end def tag_id - "#{@object_name}_#{@method_name}" + "#{sanitized_object_name}_#{@method_name}" end def tag_id_with_index(index) - "#{@object_name}_#{index}_#{@method_name}" + "#{sanitized_object_name}_#{index}_#{@method_name}" + end + + def sanitized_object_name + @object_name.gsub(/[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "") end end @@ -420,7 +424,7 @@ module ActionView @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc end - (field_helpers - %w(check_box radio_button)).each do |selector| + (field_helpers - %w(check_box radio_button fields_for)).each do |selector| src = <<-end_src def #{selector}(method, options = {}) @template.send(#{selector.inspect}, @object_name, method, options.merge(:object => @object)) @@ -428,7 +432,12 @@ module ActionView end_src class_eval src, __FILE__, __LINE__ end - + + def fields_for(name, *args, &block) + name = "#{object_name}[#{name}]" + @template.fields_for(name, *args, &block) + end + def check_box(method, options = {}, checked_value = "1", unchecked_value = "0") @template.check_box(@object_name, method, options.merge(:object => @object), checked_value, unchecked_value) end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index a7ceb6b30f..4796e3d35d 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -297,11 +297,28 @@ class FormHelperTest < Test::Unit::TestCase expected = "<form action='http://www.example.com' method='post'>" + - "<input name='post[123][title]' size='30' type='text' id='post_title' value='Hello World' />" + - "<textarea name='post[123][body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + - "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + + "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" + + "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" + "<input name='post[123][secret]' type='hidden' value='0' />" + "</form>" + + assert_dom_equal expected, _erbout + end + + def test_nested_fields_for + _erbout = '' + form_for(:post, @post) do |f| + f.fields_for(:comment, @post) do |c| + _erbout.concat c.text_field(:title) + end + end + + expected = "<form action='http://www.example.com' method='post'>" + + "<input name='post[comment][title]' size='30' type='text' id='post_comment_title' value='Hello World' />" + + "</form>" + + assert_dom_equal expected, _erbout end def test_fields_for @@ -339,6 +356,16 @@ class FormHelperTest < Test::Unit::TestCase assert_dom_equal expected, _erbout end + def test_fields_for_object_with_bracketed_name + _erbout = '' + fields_for("author[post]", @post) do |f| + _erbout.concat f.text_field(:title) + end + + assert_dom_equal "<input name='author[post][title]' size='30' type='text' id='author_post_title' value='Hello World' />", + _erbout + end + def test_form_builder_does_not_have_form_for_method assert ! ActionView::Helpers::FormBuilder.instance_methods.include?('form_for') end |