diff --git a/CHANGELOG.md b/CHANGELOG.md index 009490656..cfc2e0053 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ when the `:html5` is set to `true` [@volmer](https://github.com/volmer) ### bug fix + * Collection input that uses automatic collection translation properly sets checked values. + Closes [#971](https://github.com/plataformatec/simple_form/issues/971) [@nashby](https://github.com/nashby) * Collection input generates `required` attribute if it has `prompt` option. [@nashby](https://github.com/nashby) ## 3.0.1 diff --git a/lib/simple_form/inputs/collection_input.rb b/lib/simple_form/inputs/collection_input.rb index 3fc298506..2b1eae56b 100644 --- a/lib/simple_form/inputs/collection_input.rb +++ b/lib/simple_form/inputs/collection_input.rb @@ -90,7 +90,7 @@ def collection_includes_basic_objects?(collection_classes) def translate_collection if translated_collection = translate(:options) @collection = collection.map do |key| - [translated_collection[key] || key, key] + [translated_collection[key] || key, key.to_s] end true end diff --git a/test/inputs/collection_check_boxes_input_test.rb b/test/inputs/collection_check_boxes_input_test.rb index 6254d1a3a..41552cf28 100644 --- a/test/inputs/collection_check_boxes_input_test.rb +++ b/test/inputs/collection_check_boxes_input_test.rb @@ -53,6 +53,20 @@ class CollectionCheckBoxesInputTest < ActionView::TestCase end end + test 'input that uses automatic collection translation for check_boxes should properly set checked values' do + store_translations(:en, simple_form: { options: { defaults: { + gender: { male: 'Male', female: 'Female'} + } } } ) do + @user.gender = 'male' + + with_input_for @user, :gender, :check_boxes, collection: [:male, :female] + assert_select 'input[type=checkbox][value=male][checked=checked]' + assert_select 'input[type=checkbox][value=female]' + assert_select 'label.collection_check_boxes', 'Male' + assert_select 'label.collection_check_boxes', 'Female' + end + end + test 'input check boxes does not wrap the collection by default' do with_input_for @user, :active, :check_boxes