From 491013e06d0ad4a296cc23be6c4a48bb0a98106f Mon Sep 17 00:00:00 2001 From: Mauro George Date: Fri, 2 Jan 2015 16:53:38 -0200 Subject: Add a hidden field on the collection_radio_buttons This will avoid a error be raised when the only input on the form is the `collection_radio_buttons`. --- .../test/template/form_collections_helper_test.rb | 35 ++++++++++++++++++++++ actionview/test/template/form_helper_test.rb | 13 +++++--- 2 files changed, 44 insertions(+), 4 deletions(-) (limited to 'actionview/test/template') diff --git a/actionview/test/template/form_collections_helper_test.rb b/actionview/test/template/form_collections_helper_test.rb index b193d387c3..32471b1640 100644 --- a/actionview/test/template/form_collections_helper_test.rb +++ b/actionview/test/template/form_collections_helper_test.rb @@ -198,6 +198,41 @@ class FormCollectionsHelperTest < ActionView::TestCase assert_select 'input[type=radio][value=false][checked=checked]' end + test 'collection radio buttons generates only one hidden field for the entire collection, to ensure something will be sent back to the server when posting an empty collection' do + collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')] + with_collection_radio_buttons :user, :category_ids, collection, :id, :name + + assert_select "input[type=hidden][name='user[category_ids][]'][value='']", count: 1 + end + + test 'collection radio buttons generates a hidden field using the given :name in :html_options' do + collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')] + with_collection_radio_buttons :user, :category_ids, collection, :id, :name, {}, { name: "user[other_category_ids][]" } + + assert_select "input[type=hidden][name='user[other_category_ids][]'][value='']", count: 1 + end + + test 'collection radio buttons generates a hidden field with index if it was provided' do + collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')] + with_collection_radio_buttons :user, :category_ids, collection, :id, :name, { index: 322 } + + assert_select "input[type=hidden][name='user[322][category_ids][]'][value='']", count: 1 + end + + test 'collection radio buttons does not generate a hidden field if include_hidden option is false' do + collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')] + with_collection_radio_buttons :user, :category_ids, collection, :id, :name, include_hidden: false + + assert_select "input[type=hidden][name='user[category_ids][]'][value='']", count: 0 + end + + test 'collection radio buttons does not generate a hidden field if include_hidden option is false with key as string' do + collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')] + with_collection_radio_buttons :user, :category_ids, collection, :id, :name, 'include_hidden' => false + + assert_select "input[type=hidden][name='user[category_ids][]'][value='']", count: 0 + end + # COLLECTION CHECK BOXES test 'collection check boxes accepts a collection and generate a series of checkboxes for value method' do collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')] diff --git a/actionview/test/template/form_helper_test.rb b/actionview/test/template/form_helper_test.rb index 77c660d450..2fc381c93d 100644 --- a/actionview/test/template/form_helper_test.rb +++ b/actionview/test/template/form_helper_test.rb @@ -1596,7 +1596,8 @@ class FormHelperTest < ActionView::TestCase "" + "" + "" + - "" + "" + + "" end assert_dom_equal expected, output_buffer @@ -1619,7 +1620,8 @@ class FormHelperTest < ActionView::TestCase "true" + "" + "false" + + "" end assert_dom_equal expected, output_buffer @@ -1645,6 +1647,7 @@ class FormHelperTest < ActionView::TestCase ""+ + "" + "" end @@ -1663,7 +1666,8 @@ class FormHelperTest < ActionView::TestCase "" + "" + "" + - "" + "" + + "" end assert_dom_equal expected, output_buffer @@ -1681,7 +1685,8 @@ class FormHelperTest < ActionView::TestCase "" + "" + "" + - "" + "" + + "" end assert_dom_equal expected, output_buffer -- cgit v1.2.3