diff options
author | José Valim <jose.valim@gmail.com> | 2010-01-14 00:56:29 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-01-14 01:07:03 +0100 |
commit | 214b548485f9313639059e8de1ad08611f734fe2 (patch) | |
tree | fb6701f290abf58c31f12b4d2e0a9a094dc3a995 /actionpack | |
parent | d50bf47b008ae4c1a84ccc44e8fc7633bfffa650 (diff) | |
download | rails-214b548485f9313639059e8de1ad08611f734fe2.tar.gz rails-214b548485f9313639059e8de1ad08611f734fe2.tar.bz2 rails-214b548485f9313639059e8de1ad08611f734fe2.zip |
Make check boxes accept :multiple as option so they can handle collections (such as HABTM).
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 7 | ||||
-rw-r--r-- | actionpack/test/lib/controller/fake_models.rb | 2 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 13 |
3 files changed, 20 insertions, 2 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index ef1c51d4e7..fe060921ff 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -842,7 +842,12 @@ module ActionView checked = self.class.check_box_checked?(value(object), checked_value) end options["checked"] = "checked" if checked - add_default_name_and_id(options) + if options["multiple"] + add_default_name_and_id_for_value(checked_value, options) + options.delete("multiple") + else + add_default_name_and_id(options) + end hidden = tag("input", "name" => options["name"], "type" => "hidden", "value" => options['disabled'] && checked ? checked_value : unchecked_value) checkbox = tag("input", options) (hidden + checkbox).html_safe! diff --git a/actionpack/test/lib/controller/fake_models.rb b/actionpack/test/lib/controller/fake_models.rb index b0e5d7a94c..7346cb22bc 100644 --- a/actionpack/test/lib/controller/fake_models.rb +++ b/actionpack/test/lib/controller/fake_models.rb @@ -69,7 +69,7 @@ class Post < Struct.new(:title, :author_name, :body, :secret, :written_on, :cost attr_accessor :author def author_attributes=(attributes); end - attr_accessor :comments + attr_accessor :comments, :comment_ids def comments_attributes=(attributes); end attr_accessor :tags diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 2f6d97de3d..7712883e9c 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -245,6 +245,19 @@ class FormHelperTest < ActionView::TestCase ) end + def test_check_box_with_multiple_behavior + @post.comment_ids = [2,3] + assert_dom_equal( + '<input name="post[comment_ids][]" type="hidden" value="0" /><input id="post_comment_ids_1" name="post[comment_ids][]" type="checkbox" value="1" />', + check_box("post", "comment_ids", { :multiple => true }, 1) + ) + assert_dom_equal( + '<input name="post[comment_ids][]" type="hidden" value="0" /><input checked="checked" id="post_comment_ids_3" name="post[comment_ids][]" type="checkbox" value="3" />', + check_box("post", "comment_ids", { :multiple => true }, 3) + ) + end + + def test_checkbox_disabled_still_submits_checked_value assert_dom_equal( '<input name="post[secret]" type="hidden" value="1" /><input checked="checked" disabled="disabled" id="post_secret" name="post[secret]" type="checkbox" value="1" />', |