Skip to content

Commit eb45805

Browse files
committed
Merge pull request rails#12287 from bogdan/select-with-selected-option
Fix some edge cases for select with selected option Conflicts: actionview/CHANGELOG.md
1 parent 674e88a commit eb45805

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

actionpack/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## unreleased ##
22

3+
* Fix some edge cases for AV `select` helper with `:selected` option
4+
5+
*Bogdan Gusiev*
6+
37
* Handle `:namespace` form option in collection labels
48

59
*Vasiliy Ermolovich*

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ def select_content_tag(option_tags, options, html_options)
119119
html_options = html_options.stringify_keys
120120
add_default_name_and_id(html_options)
121121
options[:include_blank] ||= true unless options[:prompt] || select_not_required?(html_options)
122-
select = content_tag("select", add_options(option_tags, options, value(object)), html_options)
122+
value = options.fetch(:selected) { value(object) }
123+
select = content_tag("select", add_options(option_tags, options, value), html_options)
123124

124125
if html_options["multiple"] && options.fetch(:include_hidden, true)
125126
tag("input", :disabled => html_options["disabled"], :name => html_options["name"], :type => "hidden", :value => "") + select

actionpack/test/template/form_options_helper_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,22 @@ def test_select_with_disabled_value
793793
)
794794
end
795795

796+
def test_select_not_existing_method_with_selected_value
797+
@post = Post.new
798+
assert_dom_equal(
799+
"<select id=\"post_locale\" name=\"post[locale]\"><option value=\"en\">en</option>\n<option value=\"ru\" selected=\"selected\">ru</option></select>",
800+
select("post", "locale", %w( en ru ), :selected => 'ru')
801+
)
802+
end
803+
804+
def test_select_with_prompt_and_selected_value
805+
@post = Post.new
806+
assert_dom_equal(
807+
"<select id=\"post_category\" name=\"post[category]\"><option value=\"one\">one</option>\n<option selected=\"selected\" value=\"two\">two</option></select>",
808+
select("post", "category", %w( one two ), :selected => 'two', :prompt => true)
809+
)
810+
end
811+
796812
def test_select_with_disabled_array
797813
@post = Post.new
798814
@post.category = "<mus>"

0 commit comments

Comments
 (0)