Ruby-on-rails 如何更改simple_form中复选框的标签类别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5812325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to change class of a label for checkboxes in simple_form
提问by Omnipresent
using simple_formwe can change class of a label using:
使用simple_form我们可以使用以下方法更改标签的类别:
label_html => {:class => "myclass"}
label_html => {:class => "myclass"}
but how do we do the same when dealing with checkboxes?
但是在处理复选框时我们如何做同样的事情呢?
simple_formassigns the default class of collection_check_boxes
simple_form分配默认类 collection_check_boxes
Is there a way to change this default class?
有没有办法改变这个默认类?
回答by flynfish
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'
您可以使用此选项为标签指定一个类别 :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'
回答by Matteo Alessani
If you want you can pass new_classto the label doing something like:
如果你愿意,你可以传递new_class给标签做类似的事情:
<%= f.collection_check_boxes attribute, collection, value_method, text_method do |b|
b.label(class: 'new_class') {b.check_box + b.text}
end %>
回答by ipd
You should be able to set :input_html on your form input.
您应该能够在表单输入上设置 :input_html 。
Somthing like:
类似的东西:
f.input :something, :as => :check_box, :input_html => { :class => "myclass" }
ian.
伊恩。
回答by Ray Wojciechowski
The easiest way to change the label class for a checkbox is to insert the following in /config/inititializers/simple_form.rbor /config/initializers/simple_form_bootstrap.rb:
更改复选框标签类的最简单方法是在/config/initializers/simple_form.rb或/config/initializers/simple_form_bootstrap.rb 中插入以下内容:
config.boolean_label_class = 'form-check-label'
回答by d1jhoni1b
This should be pretty straight forward like mentioned above one should add :label_html => { :class => "myclass" }in order achieve this, for instance:
这应该很简单,就像上面提到的那样:label_html => { :class => "myclass" },为了实现这一点应该添加,例如:
= f.input :remember_me, as: :boolean, :input_html => { :class => 'kt-checkbox kt-mock-span' }, :label_html => { :class => "kt-login-checkbox-label" }
= 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:
将为复选框标签上的样式创建和分配属性,如下所示:
回答by awongh
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....
这是在带有简单形式 2.1 的 rails 3 中,所以 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? %>


