Skip to content
This repository
Browse code

Add :include_blank option for select_tag [#1987 status:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit 1191e3ffaf7ced4155eb6c2b1661e27d7851ca9a 1 parent ed8a0a1
Rizwan Reza authored August 08, 2009 lifo committed August 08, 2009
7  actionpack/lib/action_view/helpers/form_tag_helper.rb
@@ -79,6 +79,13 @@ def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block)
79 79
       #   #    <option>Paris</option><option>Rome</option></select>
80 80
       def select_tag(name, option_tags = nil, options = {})
81 81
         html_name = (options[:multiple] == true && !name.to_s.ends_with?("[]")) ? "#{name}[]" : name
  82
+        if blank = options.delete(:include_blank)
  83
+          if blank.kind_of?(String)
  84
+            option_tags = "<option value=\"\">#{blank}</option>" + option_tags
  85
+          else
  86
+            option_tags = "<option value=\"\"></option>" + option_tags
  87
+          end
  88
+        end
82 89
         content_tag :select, option_tags, { "name" => html_name, "id" => sanitize_to_id(name) }.update(options.stringify_keys)
83 90
       end
84 91
 
12  actionpack/test/template/form_tag_helper_test.rb
@@ -136,6 +136,18 @@ def test_select_tag_id_sanitized
136 136
     assert_match VALID_HTML_ID, input_elem['id']
137 137
   end
138 138
 
  139
+  def test_select_tag_with_include_blank
  140
+    actual = select_tag "places", "<option>Home</option><option>Work</option><option>Pub</option>", :include_blank => true
  141
+    expected = %(<select id="places" name="places"><option value=""></option><option>Home</option><option>Work</option><option>Pub</option></select>)
  142
+    assert_dom_equal expected, actual
  143
+  end
  144
+
  145
+  def test_select_tag_with_include_blank_with_string
  146
+    actual = select_tag "places", "<option>Home</option><option>Work</option><option>Pub</option>", :include_blank => "string"
  147
+    expected = %(<select id="places" name="places"><option value="">string</option><option>Home</option><option>Work</option><option>Pub</option></select>)
  148
+    assert_dom_equal expected, actual
  149
+  end
  150
+
139 151
   def test_text_area_tag_size_string
140 152
     actual = text_area_tag "body", "hello world", "size" => "20x40"
141 153
     expected = %(<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>)

0 notes on commit 1191e3f

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