how to change class of a label for checkboxes in simple_form

前端 未结 6 865
说谎
说谎 2020-12-14 15:49

using simple_form we can change class of a label using:

label_html => {:class => \"myclass\"}

but how do we do the same when

相关标签:
6条回答
  • 2020-12-14 16:01

    I wanted to give an update to this answer in case someone comes here looking for a way to do this as I did.

    You can give the label a class with this option :item_wrapper_class => 'class_goes_here'

    Here is a full example:

    = user.input :resident, 
                 :collection => [["In the U.S", true],["Outside the U.S.", false]], 
                 :label_method => :first, 
                 :value_method => :last,
                 :as => :radio_buttons, 
                 :label => "Where is your principle residence?",
                 :item_wrapper_class => 'inline'
    
    0 讨论(0)
  • 2020-12-14 16:02

    This should be pretty straight forward like mentioned above one should add :label_html => { :class => "myclass" } in order achieve this, for instance:

    = f.input :remember_me, as: :boolean, :input_html => { :class => 'kt-checkbox kt-mock-span' }, :label_html => { :class => "kt-login-checkbox-label" }

    will create and assign the attributes for the styles on the checkbox's label like this:

    0 讨论(0)
  • 2020-12-14 16:04

    To get the label class I had to get rid of the auto-generated label and write my own.

    this is in rails 3 with simple form 2.1 so YMMV....

    before:

    <%= f.input :remember_me, :as => :boolean if devise_mapping.rememberable? %>
    

    after:

    <%= f.label :remember_me, :class => 'remember-me' %>
    <%= f.input :remember_me, :label => false, :as => :boolean if devise_mapping.rememberable? %>
    
    0 讨论(0)
  • 2020-12-14 16:08

    You should be able to set :input_html on your form input.

    Somthing like:

    f.input :something, :as => :check_box, :input_html => { :class => "myclass" }
    

    ian.

    0 讨论(0)
  • 2020-12-14 16:15

    The easiest way to change the label class for a checkbox is to insert the following in /config/inititializers/simple_form.rb or /config/initializers/simple_form_bootstrap.rb:

    config.boolean_label_class = 'form-check-label'
    
    0 讨论(0)
  • 2020-12-14 16:16

    If you want you can pass new_class to the label doing something like:

    <%= f.collection_check_boxes attribute, collection, value_method, text_method do |b| 
          b.label(class: 'new_class') {b.check_box + b.text}
    end %>
    
    0 讨论(0)
提交回复
热议问题