Skip to content

Commit cb7d19b

Browse files
committed
Merge pull request rails#5414 from nashby/select-hidden-input
add 'include_hidden' option to select tag, closes rails#5402
2 parents dbdbe96 + 54a75e1 commit cb7d19b

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

actionpack/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Rails 4.0.0 (unreleased) ##
22

3+
* Add `include_hidden` option to select tag. With `:include_hidden => true` select with `multiple` attribute doesn't generate hidden input with blank value. *Vasiliy Ermolovich*
4+
35
* Removed default `size` option from the `text_field`, `search_field`, `telephone_field`, `url_field`, `email_field` helpers. *Philip Arndt*
46

57
* Removed default `cols` and `rows` options from the `text_area` helper. *Philip Arndt*

actionpack/lib/action_view/helpers/form_options_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ module FormOptionsHelper
153153
# form, and parameters extraction gets the last occurrence of any repeated
154154
# key in the query string, that works for ordinary forms.
155155
#
156+
# In case if you don't want the helper to generate this hidden field you can specify <tt>:include_blank => false</tt> option.
157+
#
156158
def select(object, method, choices, options = {}, html_options = {})
157159
Tags::Select.new(object, method, self, choices, options, html_options).render
158160
end

actionpack/lib/action_view/helpers/tags/base.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ def select_content_tag(option_tags, options, html_options)
122122
html_options = html_options.stringify_keys
123123
add_default_name_and_id(html_options)
124124
select = content_tag("select", add_options(option_tags, options, value(object)), html_options)
125-
if html_options["multiple"]
125+
126+
if html_options["multiple"] && options.fetch(:include_hidden) { true }
126127
tag("input", :disabled => html_options["disabled"], :name => html_options["name"], :type => "hidden", :value => "") + select
127128
else
128129
select

actionpack/test/template/form_options_helper_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,14 @@ def test_select_with_multiple_to_add_hidden_input
529529
)
530530
end
531531

532+
def test_select_with_multiple_and_without_hidden_input
533+
output_buffer = select(:post, :category, "", {:include_hidden => false}, :multiple => true)
534+
assert_dom_equal(
535+
"<select multiple=\"multiple\" id=\"post_category\" name=\"post[category][]\"></select>",
536+
output_buffer
537+
)
538+
end
539+
532540
def test_select_with_multiple_and_disabled_to_add_disabled_hidden_input
533541
output_buffer = select(:post, :category, "", {}, :multiple => true, :disabled => true)
534542
assert_dom_equal(

0 commit comments

Comments
 (0)