Ensure selected option is not ignored for collection_select. [#1037 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
Daniel Rodríguez Troitiño 2008-09-12 23:56:56 +02:00 committed by Pratik Naik
parent 32eeb3e521
commit 9a8e2a059c
2 changed files with 18 additions and 1 deletions

View File

@ -349,8 +349,9 @@ module ActionView
html_options = html_options.stringify_keys
add_default_name_and_id(html_options)
value = value(object)
selected_value = options.has_key?(:selected) ? options[:selected] : value
content_tag(
"select", add_options(options_from_collection_for_select(collection, value_method, text_method, value), options, value), html_options
"select", add_options(options_from_collection_for_select(collection, value_method, text_method, selected_value), options, value), html_options
)
end

View File

@ -473,6 +473,22 @@ uses_mocha "FormOptionsHelperTest" do
assert_dom_equal expected, collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true, :name => 'post[author_name][]' }, :multiple => true)
end
def test_collection_select_with_blank_and_selected
@posts = [
Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
Post.new("Babe went home", "Babe", "To a little house", "shh!"),
Post.new("Cabe went home", "Cabe", "To a little house", "shh!")
]
@post = Post.new
@post.author_name = "Babe"
assert_dom_equal(
%{<select id="post_author_name" name="post[author_name]"><option value=""></option>\n<option value="&lt;Abe&gt;" selected="selected">&lt;Abe&gt;</option>\n<option value="Babe">Babe</option>\n<option value="Cabe">Cabe</option></select>},
collection_select("post", "author_name", @posts, "author_name", "author_name", {:include_blank => true, :selected => "<Abe>"})
)
end
def test_time_zone_select
@firm = Firm.new("D")
html = time_zone_select( "firm", "time_zone" )