Using Simple_form 2.0.2
The simple form code using HAML:
= f.input :remember_me, as: :boolean, inline_label: 'Remember me'
But it renders this:
<div class="control-group boolean optional">
<label class="boolean optional control-label" for="admin_remember_me">Remember me</label>
<div class="controls">
<input name="admin[remember_me]" type="hidden" value="0" />
<label class="checkbox"><input class="boolean optional" id="admin_remember_me" name="admin[remember_me]" type="checkbox" value="1" />Remember me</label>
</div>
</div>
How do I remove that first label that's rendered, so that I only have the inline label?
You can use:
= f.input :remember_me, as: :boolean, inline_label: 'Remember me', label: false
Found a solution after much Google fu.
Use input_field
instead of input
which won't automatically generate a label.
= f.input_field :remember_me, as: :boolean, inline_label: 'Remember me'
For whom it doesn't work
= f.input_field ...
Use this way
= f.check_box ...
With simple_form 2.1.0 and rails 3.0.20, none of the solutions listed here worked (I don't want to use f.input_field because it's an admission of defeat).
The missing part is the boolean_style option:
options.merge!({label: false, boolean_style: :inline})
I suggest you create a custom input for this (e.g.: inline_checkbox)
boolean_style is configured as :nested by default, I think:
# Defaults to :nested for bootstrap config.
# :inline => input + label
# :nested => label > input
config.boolean_style = :nested
.control-group.error .help-inline {
display: none;
}
This should work, it works for me on rails 3.2 and simple_form 2.x+
Maybe too late, but inspired by gamov answer I have made this a custom wrapper from inline bootstrap checkbox in the initializer file 'config/simple_form_bootstrap.rb':
config.wrappers :horizontal_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.optional :readonly
b.use :label, class: 'col-sm-3 control-label'
b.use :input
b.use :error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
which generates this html:
<div class="form-group boolean optional user_admin">
<label class="boolean optional col-sm-3 control-label" for="user_admin">Admin</label>
<div class="col-sm-9 checkbox-inline">
<input name="user[admin]" value="0" type="hidden">
<input class="boolean optional" id="user_admin" name="user[admin]" value="1" type="checkbox">
</div>
来源:https://stackoverflow.com/questions/12114014/simple-form-remove-outer-label-for-an-inline-checkbox-with-label