diff options
author | Daniel RodrÃguez Troitiño <drodrigueztroitino@yahoo.es> | 2008-09-12 23:56:56 +0200 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-01-28 19:35:18 +0000 |
commit | 9a8e2a059c823498e0fb318f25b926a6d015ef87 (patch) | |
tree | bae72e71d9e754500f56688aef61e27ba88dc52d /actionpack | |
parent | 32eeb3e5211a4a7bfc7a1d0aa0cab1486bed3581 (diff) | |
download | rails-9a8e2a059c823498e0fb318f25b926a6d015ef87.tar.gz rails-9a8e2a059c823498e0fb318f25b926a6d015ef87.tar.bz2 rails-9a8e2a059c823498e0fb318f25b926a6d015ef87.zip |
Ensure selected option is not ignored for collection_select. [#1037 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_view/helpers/form_options_helper.rb | 3 | ||||
-rw-r--r-- | actionpack/test/template/form_options_helper_test.rb | 16 |
2 files changed, 18 insertions, 1 deletions
diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 9ed50a9653..3991c5c0cc 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -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 diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 86a0bb6a79..78548864ff 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -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="<Abe>" selected="selected"><Abe></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" ) |