Skip to content

Commit c2ad51a

Browse files
nashbysgrif
authored andcommitted
Collection check boxes propagates input's id to the label's for attribute.
1 parent 7e434d6 commit c2ad51a

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

actionview/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Collection input propagates input's `id` to the label's `for` attribute when
2+
using html options as the last element of collection.
3+
4+
*Vasiliy Ermolovich*
5+
16
* Add a `hidden_field` on the `collection_radio_buttons` to avoid raising a error
27
when the only input on the form is the `collection_radio_buttons`.
38

actionview/lib/action_view/helpers/tags/collection_helpers.rb

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def initialize(template_object, object_name, method_name, object,
1919

2020
def label(label_html_options={}, &block)
2121
html_options = @input_html_options.slice(:index, :namespace).merge(label_html_options)
22+
html_options[:for] ||= @input_html_options[:id] if @input_html_options[:id]
23+
2224
@template_object.label(@object_name, @sanitized_attribute_name, @text, html_options, &block)
2325
end
2426
end

actionview/test/template/form_collections_helper_test.rb

+11
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,17 @@ def with_collection_check_boxes(*args, &block)
306306
assert_select 'input[type=checkbox][value="2"].bar'
307307
end
308308

309+
test 'collection check boxes propagates input id to the label for attribute' do
310+
collection = [[1, 'Category 1', {id: 'foo'}], [2, 'Category 2', {id: 'bar'}]]
311+
with_collection_check_boxes :user, :active, collection, :first, :second
312+
313+
assert_select 'input[type=checkbox][value="1"]#foo'
314+
assert_select 'input[type=checkbox][value="2"]#bar'
315+
316+
assert_select 'label[for=foo]'
317+
assert_select 'label[for=bar]'
318+
end
319+
309320
test 'collection check boxes sets the label class defined inside the block' do
310321
collection = [[1, 'Category 1', {class: 'foo'}], [2, 'Category 2', {class: 'bar'}]]
311322
with_collection_check_boxes :user, :active, collection, :second, :first do |b|

0 commit comments

Comments
 (0)