Skip to content
This repository
Browse code

Fixed bug creating invalid HTML in select options

When a select tag is created for a field with errors, then the inserted
options will errantly have a <div class="field_with_errors"> wrapping
them.
  • Loading branch information...
commit 1a935ad7c647534f83ec03388ea1157b39eb6dcb 1 parent 836016e
rustygeldmacher authored July 10, 2012
2  actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -353,7 +353,7 @@ def options_for_select(container, selected = nil)
353 353
           html_attributes[:disabled] = 'disabled' if disabled && option_value_selected?(value, disabled)
354 354
           html_attributes[:value] = value
355 355
 
356  
-          content_tag(:option, text, html_attributes)
  356
+          content_tag_string(:option, text, html_attributes)
357 357
         end.join("\n").html_safe
358 358
       end
359 359
 
4  actionpack/lib/action_view/helpers/tags/base.rb
@@ -137,10 +137,10 @@ def select_not_required?(html_options)
137 137
 
138 138
         def add_options(option_tags, options, value = nil)
139 139
           if options[:include_blank]
140  
-            option_tags = content_tag('option', options[:include_blank].kind_of?(String) ? options[:include_blank] : nil, :value => '') + "\n" + option_tags
  140
+            option_tags = content_tag_string('option', options[:include_blank].kind_of?(String) ? options[:include_blank] : nil, :value => '') + "\n" + option_tags
141 141
           end
142 142
           if value.blank? && options[:prompt]
143  
-            option_tags = content_tag('option', prompt_text(options[:prompt]), :value => '') + "\n" + option_tags
  143
+            option_tags = content_tag_string('option', prompt_text(options[:prompt]), :value => '') + "\n" + option_tags
144 144
           end
145 145
           option_tags
146 146
         end
14  actionpack/test/template/active_model_helper_test.rb
@@ -41,6 +41,19 @@ def test_text_field_with_errors
41 41
     )
42 42
   end
43 43
 
  44
+  def test_select_with_errors
  45
+    assert_dom_equal(
  46
+      %(<div class="field_with_errors"><select name="post[author_name]" id="post_author_name"><option value="a">a</option>\n<option value="b">b</option></select></div>),
  47
+      select("post", "author_name", [:a, :b])
  48
+    )
  49
+  end
  50
+
  51
+  def test_select_with_errors_and_blank_option
  52
+    expected_dom = %(<div class="field_with_errors"><select name="post[author_name]" id="post_author_name"><option value="">Choose one...</option>\n<option value="a">a</option>\n<option value="b">b</option></select></div>)
  53
+    assert_dom_equal(expected_dom, select("post", "author_name", [:a, :b], :include_blank => 'Choose one...'))
  54
+    assert_dom_equal(expected_dom, select("post", "author_name", [:a, :b], :prompt => 'Choose one...'))
  55
+  end
  56
+
44 57
   def test_date_select_with_errors
45 58
     assert_dom_equal(
46 59
       %(<div class="field_with_errors"><select id="post_updated_at_1i" name="post[updated_at(1i)]">\n<option selected="selected" value="2004">2004</option>\n<option value="2005">2005</option>\n</select>\n<input id="post_updated_at_2i" name="post[updated_at(2i)]" type="hidden" value="6" />\n<input id="post_updated_at_3i" name="post[updated_at(3i)]" type="hidden" value="1" />\n</div>),
@@ -82,4 +95,5 @@ def test_field_error_proc
82 95
   ensure
83 96
     ActionView::Base.field_error_proc = old_proc if old_proc
84 97
   end
  98
+
85 99
 end

0 notes on commit 1a935ad

Please sign in to comment.
Something went wrong with that request. Please try again.