Skip to content

Commit 6617d01

Browse files
zilkeyjosevalim
authored andcommitted
Sending :id => nil to form helpers now properly omits the "id" html element [rails#4559 state:resolved]
Signed-off-by: José Valim <[email protected]>
1 parent d6cbb27 commit 6617d01

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-5
lines changed

actionpack/lib/action_view/helpers/form_helper.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,13 @@ def to_label_tag(text = nil, options = {}, &block)
859859
options = options.stringify_keys
860860
tag_value = options.delete("value")
861861
name_and_id = options.dup
862-
name_and_id["id"] = name_and_id["for"]
862+
863+
if name_and_id["for"]
864+
name_and_id["id"] = name_and_id["for"]
865+
else
866+
name_and_id.delete("id")
867+
end
868+
863869
add_default_name_and_id_for_value(tag_value, name_and_id)
864870
options.delete("index")
865871
options["for"] ||= name_and_id["id"]
@@ -1027,7 +1033,7 @@ def add_default_name_and_id_for_value(tag_value, options)
10271033
pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase
10281034
specified_id = options["id"]
10291035
add_default_name_and_id(options)
1030-
options["id"] += "_#{pretty_tag_value}" unless specified_id
1036+
options["id"] += "_#{pretty_tag_value}" if specified_id.blank? && options["id"].present?
10311037
else
10321038
add_default_name_and_id(options)
10331039
end
@@ -1036,14 +1042,14 @@ def add_default_name_and_id_for_value(tag_value, options)
10361042
def add_default_name_and_id(options)
10371043
if options.has_key?("index")
10381044
options["name"] ||= tag_name_with_index(options["index"])
1039-
options["id"] ||= tag_id_with_index(options["index"])
1045+
options["id"] = options.fetch("id", tag_id_with_index(options["index"]))
10401046
options.delete("index")
10411047
elsif defined?(@auto_index)
10421048
options["name"] ||= tag_name_with_index(@auto_index)
1043-
options["id"] ||= tag_id_with_index(@auto_index)
1049+
options["id"] = options.fetch("id", tag_id_with_index(@auto_index))
10441050
else
10451051
options["name"] ||= tag_name + (options.has_key?('multiple') ? '[]' : '')
1046-
options["id"] ||= tag_id
1052+
options["id"] = options.fetch("id", tag_id)
10471053
end
10481054
end
10491055

actionpack/test/template/form_helper_test.rb

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,90 @@ def test_explicit_id
423423
check_box("post", "secret", :id => "i mean it")
424424
end
425425

426+
def test_nil_id
427+
assert_dom_equal(
428+
'<input name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => nil)
429+
)
430+
assert_dom_equal(
431+
'<textarea cols="40" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
432+
text_area("post", "body", "id" => nil)
433+
)
434+
assert_dom_equal(
435+
'<input name="post[secret]" type="hidden" value="0" /><input checked="checked" name="post[secret]" type="checkbox" value="1" />',
436+
check_box("post", "secret", "id" => nil)
437+
)
438+
assert_dom_equal(
439+
'<input type="radio" name="post[secret]" value="0" />',
440+
radio_button("post", "secret", "0", "id" => nil)
441+
)
442+
assert_dom_equal(
443+
'<select name="post[secret]"></select>',
444+
select("post", "secret", [], {}, "id" => nil)
445+
)
446+
assert_dom_equal text_field("post", "title", "id" => nil),
447+
text_field("post", "title", :id => nil)
448+
assert_dom_equal text_area("post", "body", "id" => nil),
449+
text_area("post", "body", :id => nil)
450+
assert_dom_equal check_box("post", "secret", "id" => nil),
451+
check_box("post", "secret", :id => nil)
452+
assert_dom_equal radio_button("post", "secret", "id" => nil),
453+
radio_button("post", "secret", :id => nil)
454+
end
455+
456+
def test_index
457+
assert_dom_equal(
458+
'<input name="post[5][title]" size="30" id="post_5_title" type="text" value="Hello World" />',
459+
text_field("post", "title", "index" => 5)
460+
)
461+
assert_dom_equal(
462+
'<textarea cols="40" name="post[5][body]" id="post_5_body" rows="20">Back to the hill and over it again!</textarea>',
463+
text_area("post", "body", "index" => 5)
464+
)
465+
assert_dom_equal(
466+
'<input name="post[5][secret]" type="hidden" value="0" /><input checked="checked" name="post[5][secret]" type="checkbox" value="1" id="post_5_secret" />',
467+
check_box("post", "secret", "index" => 5)
468+
)
469+
assert_dom_equal(
470+
text_field("post", "title", "index" => 5),
471+
text_field("post", "title", "index" => 5)
472+
)
473+
assert_dom_equal(
474+
text_area("post", "body", "index" => 5),
475+
text_area("post", "body", "index" => 5)
476+
)
477+
assert_dom_equal(
478+
check_box("post", "secret", "index" => 5),
479+
check_box("post", "secret", "index" => 5)
480+
)
481+
end
482+
483+
def test_index_with_nil_id
484+
assert_dom_equal(
485+
'<input name="post[5][title]" size="30" type="text" value="Hello World" />',
486+
text_field("post", "title", "index" => 5, 'id' => nil)
487+
)
488+
assert_dom_equal(
489+
'<textarea cols="40" name="post[5][body]" rows="20">Back to the hill and over it again!</textarea>',
490+
text_area("post", "body", "index" => 5, 'id' => nil)
491+
)
492+
assert_dom_equal(
493+
'<input name="post[5][secret]" type="hidden" value="0" /><input checked="checked" name="post[5][secret]" type="checkbox" value="1" />',
494+
check_box("post", "secret", "index" => 5, 'id' => nil)
495+
)
496+
assert_dom_equal(
497+
text_field("post", "title", "index" => 5, 'id' => nil),
498+
text_field("post", "title", :index => 5, :id => nil)
499+
)
500+
assert_dom_equal(
501+
text_area("post", "body", "index" => 5, 'id' => nil),
502+
text_area("post", "body", :index => 5, :id => nil)
503+
)
504+
assert_dom_equal(
505+
check_box("post", "secret", "index" => 5, 'id' => nil),
506+
check_box("post", "secret", :index => 5, :id => nil)
507+
)
508+
end
509+
426510
def test_auto_index
427511
pid = @post.id
428512
assert_dom_equal(
@@ -449,6 +533,29 @@ def test_auto_index
449533
)
450534
end
451535

536+
def test_auto_index_with_nil_id
537+
pid = @post.id
538+
assert_dom_equal(
539+
"<input name=\"post[#{pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />",
540+
text_field("post[]","title", :id => nil)
541+
)
542+
assert_dom_equal(
543+
"<textarea cols=\"40\" name=\"post[#{pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
544+
text_area("post[]", "body", :id => nil)
545+
)
546+
assert_dom_equal(
547+
"<input name=\"post[#{pid}][secret]\" type=\"hidden\" value=\"0\" /><input checked=\"checked\" name=\"post[#{pid}][secret]\" type=\"checkbox\" value=\"1\" />",
548+
check_box("post[]", "secret", :id => nil)
549+
)
550+
assert_dom_equal(
551+
"<input checked=\"checked\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Hello World\" />",
552+
radio_button("post[]", "title", "Hello World", :id => nil)
553+
)
554+
assert_dom_equal("<input name=\"post[#{pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
555+
radio_button("post[]", "title", "Goodbye World", :id => nil)
556+
)
557+
end
558+
452559
def test_form_for
453560
assert_deprecated do
454561
form_for(:post, @post, :html => { :id => 'create-post' }) do |f|

0 commit comments

Comments
 (0)