diff --git a/actionview/lib/action_view/helpers/tag_helper.rb b/actionview/lib/action_view/helpers/tag_helper.rb index 0944acba2206fe28aad1e682916608bf2f656254..ffb364dce16684ea6fcadb8dff12195a64ebe4d3 100644 --- a/actionview/lib/action_view/helpers/tag_helper.rb +++ b/actionview/lib/action_view/helpers/tag_helper.rb @@ -94,7 +94,7 @@ def boolean_tag_option(key) def tag_option(key, value, escape) case value when Array, Hash - value = build_tag_values(value) + value = build_tag_values(value) if key.to_s == "class" value = escape ? safe_join(value, " ") : value.join(" ") else value = escape ? ERB::Util.unwrapped_html_escape(value) : value.to_s @@ -109,14 +109,14 @@ def build_tag_values(*args) args.each do |tag_value| case tag_value - when String - tag_values << tag_value if tag_value.present? when Hash tag_value.each do |key, val| tag_values << key if val end when Array tag_values << build_tag_values(*tag_value).presence + else + tag_values << tag_value.to_s if tag_value.present? end end diff --git a/actionview/test/template/tag_helper_test.rb b/actionview/test/template/tag_helper_test.rb index 93d923055cabbe918e6d638366d7967d1ebe8e65..44496bce9b56fd7b772b16c67c8b5a1bb9a98db5 100644 --- a/actionview/test/template/tag_helper_test.rb +++ b/actionview/test/template/tag_helper_test.rb @@ -37,6 +37,24 @@ def test_tag_options assert_match(/class="elsewhere"/, str) end + def test_tag_options_with_array_of_numeric + str = tag(:input, value: [123, 456]) + + assert_equal("", str) + end + + def test_tag_options_with_array_of_random_objects + klass = Class.new do + def to_s + "hello" + end + end + + str = tag(:input, value: [klass.new]) + + assert_equal("", str) + end + def test_tag_options_rejects_nil_option assert_equal "

", tag("p", ignored: nil) end @@ -262,6 +280,18 @@ def test_content_tag_with_conditional_hash_classes str = content_tag("p", "limelight", class: ["song", { foo: false }]) assert_equal "

limelight

", str + str = content_tag("p", "limelight", class: [1, 2, 3]) + assert_equal "

limelight

", str + + klass = Class.new do + def to_s + "1" + end + end + + str = content_tag("p", "limelight", class: [klass.new]) + assert_equal "

limelight

", str + str = content_tag("p", "limelight", class: { "song": true, "play": true }) assert_equal "

limelight

", str