1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Collection check boxes propagates input's id to the label's for attribute.

This commit is contained in:
Vasiliy Ermolovich 2015-06-08 23:55:10 +03:00 committed by Sean Griffin
parent 7e434d6d2d
commit c2ad51a2d0
3 changed files with 18 additions and 0 deletions

View file

@ -1,3 +1,8 @@
* Collection input propagates input's `id` to the label's `for` attribute when
using html options as the last element of collection.
*Vasiliy Ermolovich*
* Add a `hidden_field` on the `collection_radio_buttons` to avoid raising a error
when the only input on the form is the `collection_radio_buttons`.

View file

@ -19,6 +19,8 @@ module ActionView
def label(label_html_options={}, &block)
html_options = @input_html_options.slice(:index, :namespace).merge(label_html_options)
html_options[:for] ||= @input_html_options[:id] if @input_html_options[:id]
@template_object.label(@object_name, @sanitized_attribute_name, @text, html_options, &block)
end
end

View file

@ -306,6 +306,17 @@ class FormCollectionsHelperTest < ActionView::TestCase
assert_select 'input[type=checkbox][value="2"].bar'
end
test 'collection check boxes propagates input id to the label for attribute' do
collection = [[1, 'Category 1', {id: 'foo'}], [2, 'Category 2', {id: 'bar'}]]
with_collection_check_boxes :user, :active, collection, :first, :second
assert_select 'input[type=checkbox][value="1"]#foo'
assert_select 'input[type=checkbox][value="2"]#bar'
assert_select 'label[for=foo]'
assert_select 'label[for=bar]'
end
test 'collection check boxes sets the label class defined inside the block' do
collection = [[1, 'Category 1', {class: 'foo'}], [2, 'Category 2', {class: 'bar'}]]
with_collection_check_boxes :user, :active, collection, :second, :first do |b|