diff --git a/lib/simple_form/components/labels.rb b/lib/simple_form/components/labels.rb index 2cf4f781..3349554b 100644 --- a/lib/simple_form/components/labels.rb +++ b/lib/simple_form/components/labels.rb @@ -38,7 +38,7 @@ module SimpleForm end def label_html_options - label_options = html_options_for(:label, [input_type, required_class, SimpleForm.label_class]) + label_options = html_options_for(:label, [input_type, required_class, SimpleForm.label_class].compact) label_options[:for] = options[:input_html][:id] if options.key?(:input_html) && options[:input_html].key?(:id) label_options end diff --git a/test/components/label_test.rb b/test/components/label_test.rb index 7cef72f1..87fecf5b 100644 --- a/test/components/label_test.rb +++ b/test/components/label_test.rb @@ -296,4 +296,13 @@ class IsolatedLabelTest < ActionView::TestCase assert_select 'label.my_custom_class' end end + + test 'label strips extra classes even when label_class is nil' do + swap SimpleForm, :label_class => nil do + with_label_for @user, :name, :string + assert_select "label[class='string required']" + assert_no_select "label[class='string required ']" + assert_no_select "label[class=' string required']" + end + end end