Simple_form: Remove outer label for an inline checkbox with label

前端 未结 6 1469
盖世英雄少女心
盖世英雄少女心 2021-02-02 07:14

Using Simple_form 2.0.2

The simple form code using HAML:

= f.input :remember_me, as: :boolean, inline_label: \'Remember me\'

But it ren

相关标签:
6条回答
  • 2021-02-02 07:51

    For whom it doesn't work

    = f.input_field ...

    Use this way

    = f.check_box ...

    0 讨论(0)
  • 2021-02-02 07:51

    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
    
    0 讨论(0)
  • 2021-02-02 07:52

    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'
    
    0 讨论(0)
  • 2021-02-02 07:58
    .control-group.error .help-inline {
      display: none;
    }
    

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

    0 讨论(0)
  • 2021-02-02 08:01

    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>
    

    0 讨论(0)
  • 2021-02-02 08:08

    You can use:

    = f.input :remember_me, as: :boolean, inline_label: 'Remember me', label: false
    
    0 讨论(0)
提交回复
热议问题