Skip to content
This repository
Browse code

Ensure selected option is not ignored for collection_select. [#1037 s…

…tate:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit 9a8e2a059c823498e0fb318f25b926a6d015ef87 1 parent 32eeb3e
Daniel Rodríguez Troitiño authored September 12, 2008 lifo committed January 28, 2009
3  actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -349,8 +349,9 @@ def to_collection_select_tag(collection, value_method, text_method, options, htm
349 349
         html_options = html_options.stringify_keys
350 350
         add_default_name_and_id(html_options)
351 351
         value = value(object)
  352
+        selected_value = options.has_key?(:selected) ? options[:selected] : value
352 353
         content_tag(
353  
-          "select", add_options(options_from_collection_for_select(collection, value_method, text_method, value), options, value), html_options
  354
+          "select", add_options(options_from_collection_for_select(collection, value_method, text_method, selected_value), options, value), html_options
354 355
         )
355 356
       end
356 357
 
16  actionpack/test/template/form_options_helper_test.rb
@@ -473,6 +473,22 @@ def test_collection_select_with_multiple_option_appends_array_brackets
473 473
       assert_dom_equal expected, collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true, :name => 'post[author_name][]' }, :multiple => true)
474 474
     end
475 475
 
  476
+    def test_collection_select_with_blank_and_selected
  477
+      @posts = [
  478
+        Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
  479
+        Post.new("Babe went home", "Babe", "To a little house", "shh!"),
  480
+        Post.new("Cabe went home", "Cabe", "To a little house", "shh!")
  481
+      ]
  482
+
  483
+      @post = Post.new
  484
+      @post.author_name = "Babe"
  485
+
  486
+      assert_dom_equal(
  487
+        %{<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>},
  488
+        collection_select("post", "author_name", @posts, "author_name", "author_name", {:include_blank => true, :selected => "<Abe>"})
  489
+      )
  490
+    end
  491
+
476 492
     def test_time_zone_select
477 493
       @firm = Firm.new("D")
478 494
       html = time_zone_select( "firm", "time_zone" )

0 notes on commit 9a8e2a0

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