simple_form 的 collection_radio_button 和自定义标签类 [英] simple_form's collection_radio_button and custom label class
问题描述
我正在尝试使用 FontAwesome 制作带有无线电集合的星级评定表,为此我实际上需要更改由 simple_form 生成的 collection_radio_button 输入的标签类,但找不到任何明显的解决方案.
I'm trying to make a star rating form with radio collection using FontAwesome, for this I actually need to change the label classes of the collection_radio_button input generated by simple_form but can't find any obvious solution.
到目前为止我使用:
form_for @user do |f|
f.collection_radio_buttons :rating, [[1, 'Bad'] ,[2, 'Ok'], [3, 'Great']],
:first, :last, { item_wrapper_tag: false }
end
产生:
<input id="review_rating_1" name="review[rating]" type="radio" value="1" />
<label class="collection_radio_buttons" for="review_rating_1">Bad</label>
<input id="review_rating_2" name="review[rating]" type="radio" value="2" />
<label class="collection_radio_buttons" for="review_rating_2">Ok</label>
<input id="review_rating_3" name="user[options]" type="radio" value="3" />
<label class="collection_radio_buttons" for="review_rating_3">Great</label>
但我希望标签有一个额外的类,例如:
But I'd like the labels to have an extra class, like:
<input id="review_rating_3" name="user[options]" type="radio" value="3" />
<label class="collection_radio_buttons icon-star" for="review_rating_3">Great</label>
更新:该类在以下位置静态定义:https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/tags.rb#L43
UPDATE: This class is defined statically at: https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/tags.rb#L43
推荐答案
这可以通过使用块来实现:
This can be achieved by using a block:
form_for @user do |f|
f.collection_radio_buttons :rating, [[1, 'Bad'] ,[2, 'Ok'], [3, 'Great']],
:first, :last, { item_wrapper_tag: false } do |b|
b.radio_button + b.label(:class => "collection_radio_buttons icon-star")
end
end
此文档可以展示一些其他示例:http://rubydoc.info/github/plataformatec/simple_form/SimpleForm/FormBuilder:collection_radio_buttons
This doc can showcase some other example: http://rubydoc.info/github/plataformatec/simple_form/SimpleForm/FormBuilder:collection_radio_buttons
这篇关于simple_form 的 collection_radio_button 和自定义标签类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!