From b69d33752703b36c1b8b5ccf043fd80a5d323d82 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 24 Jan 2005 15:35:30 +0000 Subject: Applied the dblack patch git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@499 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_view/helpers/form_helper.rb | 6 ++++++ actionpack/test/template/form_helper_test.rb | 26 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index bed9eae244..36e1f946f8 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -137,6 +137,9 @@ module ActionView def initialize(object_name, method_name, template_object, local_binding = nil) @object_name, @method_name = object_name, method_name @template_object, @local_binding = template_object, local_binding + if @object_name.sub!(/\[\]$/,"") + @auto_index = @template_object.instance_variable_get("@#{Regexp.last_match.pre_match}").id + end end def to_input_field_tag(field_type, options = {}) @@ -214,6 +217,9 @@ module ActionView options['name'] = tag_name_with_index(options["index"]) unless options.has_key? "name" options['id'] = tag_id_with_index(options["index"]) unless options.has_key? "id" options.delete("index") + elsif @auto_index + options['name'] = tag_name_with_index(@auto_index) unless options.has_key? "name" + options['id'] = tag_id_with_index(@auto_index) unless options.has_key? "id" else options['name'] = tag_name unless options.has_key? "name" options['id'] = tag_id unless options.has_key? "id" diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index c6128a9337..6b1deb575e 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -18,6 +18,8 @@ class FormHelperTest < Test::Unit::TestCase @post = Post.new def @post.errors() Class.new{ def on(field) field == "author_name" end }.new end + def @post.id; 123; end + @post.title = "Hello World" @post.author_name = "" @post.body = "Back to the hill and over it again!" @@ -136,4 +138,28 @@ class FormHelperTest < Test::Unit::TestCase check_box("post", "secret", "id" => "i mean it") ) end + + def test_auto_index + pid = @post.id + assert_equal( + "", text_field("post[]","title") + ) + assert_equal( + "", + text_area("post[]", "body") + ) + assert_equal( + "", + check_box("post[]", "secret") + ) + assert_equal( +"", + radio_button("post[]", "title", "Hello World") + ) + assert_equal("", + radio_button("post[]", "title", "Goodbye World") + ) + + end + end -- cgit v1.2.3