38

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?

David Nix
  • 3,324
  • 3
  • 32
  • 51

6 Answers6

90

You can use:

= f.input :remember_me, as: :boolean, inline_label: 'Remember me', label: false
Hernan S.
  • 2,604
  • 3
  • 17
  • 15
27

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'
David Nix
  • 3,324
  • 3
  • 32
  • 51
11

For whom it doesn't work

= f.input_field ...

Use this way

= f.check_box ...

Sergiy Seletskyy
  • 16,236
  • 7
  • 69
  • 80
7

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
gamov
  • 3,789
  • 1
  • 31
  • 28
0
.control-group.error .help-inline {
  display: none;
}

This should work, it works for me on rails 3.2 and simple_form 2.x+

Dreamr OKelly
  • 3,544
  • 1
  • 14
  • 3
0

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>

Marino
  • 106
  • 9