diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index f840998b7aae259bfc3cfb5e610b610da044e81d..8df09b662b8b85cee5eeed9596bd730d3d6a2204 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -768,7 +768,11 @@ def to_label_tag(text = nil, options = {}) options = options.stringify_keys tag_value = options.delete("value") name_and_id = options.dup - name_and_id["id"] = name_and_id["for"] + if name_and_id.has_key?("for") + name_and_id["id"] = name_and_id["for"] + else + name_and_id.delete("id") + end add_default_name_and_id_for_value(tag_value, name_and_id) options.delete("index") options["for"] ||= name_and_id["id"] @@ -928,15 +932,15 @@ def add_default_name_and_id_for_value(tag_value, options) def add_default_name_and_id(options) if options.has_key?("index") - options["name"] ||= tag_name_with_index(options["index"]) - options["id"] ||= tag_id_with_index(options["index"]) + 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 defined?(@auto_index) - options["name"] ||= tag_name_with_index(@auto_index) - options["id"] ||= tag_id_with_index(@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 + (options.has_key?('multiple') ? '[]' : '') - options["id"] ||= tag_id + options["name"] = tag_name + (options.has_key?('multiple') ? '[]' : '') unless options.has_key?("name") + options["id"] = tag_id unless options.has_key?("id") end end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 45d424134ef0c31a9d9af3f30d3d70fa70dfa6f3..1cdd83587567fd3a692a25f754c71afb4ef3baa9 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -175,6 +175,10 @@ def test_label_with_locales_symbols I18n.locale = old_locale end + def test_label_with_for_attribute_as_nil + assert_dom_equal('', label(:post, :title, nil, :for => nil)) + end + def test_label_with_for_attribute_as_symbol assert_dom_equal('', label(:post, :title, nil, :for => "my_for")) end @@ -274,6 +278,11 @@ def test_text_field_with_options hidden_field("post", "title", :value => "Something Else") end + def test_text_field_with_id_as_nil + assert_dom_equal '', + hidden_field("post", "title", :id => nil) + end + def test_check_box assert_dom_equal( '',