Skip to content
This repository
Browse code

Fix collection_check_boxes and collection_radio_buttons when using local

variables in the form builder
  • Loading branch information...
commit 2ff884c74888e9133120782b9be14e3ef97f3958 1 parent 80680e9
Rafael Mendonça França authored February 26, 2012
1  actionpack/lib/action_view/helpers/tags/collection_helpers.rb
@@ -59,6 +59,7 @@ def default_html_options_for_collection(item, value) #:nodoc:
59 59
             end
60 60
           end
61 61
 
  62
+          html_options[:object] = @object
62 63
           html_options
63 64
         end
64 65
 
38  actionpack/test/template/form_helper_test.rb
@@ -771,6 +771,44 @@ def test_form_for
771 771
     assert_dom_equal expected, output_buffer
772 772
   end
773 773
 
  774
+  def test_form_for_with_collection_radio_buttons
  775
+    post = Post.new
  776
+    def post.active; false; end
  777
+    form_for(post) do |f|
  778
+      concat f.collection_radio_buttons(:active, [true, false], :to_s, :to_s)
  779
+    end
  780
+
  781
+    expected = whole_form("/posts", "new_post" , "new_post") do
  782
+      "<input id='post_active_true' name='post[active]' type='radio' value='true' />" +
  783
+      "<label for='post_active_true'>true</label>" +
  784
+      "<input checked='checked' id='post_active_false' name='post[active]' type='radio' value='false' />" +
  785
+      "<label for='post_active_false'>false</label>"
  786
+    end
  787
+
  788
+    assert_dom_equal expected, output_buffer
  789
+  end
  790
+
  791
+  def test_form_for_with_collection_check_boxes
  792
+    post = Post.new
  793
+    def post.tag_ids; [1, 3]; end
  794
+    collection = (1..3).map{|i| [i, "Tag #{i}"] }
  795
+    form_for(post) do |f|
  796
+      concat f.collection_check_boxes(:tag_ids, collection, :first, :last)
  797
+    end
  798
+
  799
+    expected = whole_form("/posts", "new_post" , "new_post") do
  800
+      "<input checked='checked' id='post_tag_ids_1' name='post[tag_ids][]' type='checkbox' value='1' />" +
  801
+      "<label for='post_tag_ids_1'>Tag 1</label>" +
  802
+      "<input id='post_tag_ids_2' name='post[tag_ids][]' type='checkbox' value='2' />" +
  803
+      "<label for='post_tag_ids_2'>Tag 2</label>" +
  804
+      "<input checked='checked' id='post_tag_ids_3' name='post[tag_ids][]' type='checkbox' value='3' />" +
  805
+      "<label for='post_tag_ids_3'>Tag 3</label>" +
  806
+      "<input name='post[tag_ids][]' type='hidden' value='' />"
  807
+    end
  808
+
  809
+    assert_dom_equal expected, output_buffer
  810
+  end
  811
+
774 812
   def test_form_for_with_file_field_generate_multipart
775 813
     Post.send :attr_accessor, :file
776 814
 

0 notes on commit 2ff884c

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