Simple_form: Remove outer label for an inline checkbox with label

拜拜、爱过 提交于 2019-12-02 20:11:13

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>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!