Ruby 简单形式';s collection_单选按钮和自定义标签类
我正在尝试使用FontAwesome制作一个带有radio collection的星级表,为此我实际上需要更改由simple_form生成的collection_radio_按钮输入的标签类,但找不到任何明显的解决方案 到目前为止,我使用:Ruby 简单形式';s collection_单选按钮和自定义标签类,ruby,simple-form,Ruby,Simple Form,我正在尝试使用FontAwesome制作一个带有radio collection的星级表,为此我实际上需要更改由simple_form生成的collection_radio_按钮输入的标签类,但找不到任何明显的解决方案 到目前为止,我使用: form_for @user do |f| f.collection_radio_buttons :rating, [[1, 'Bad'] ,[2, 'Ok'], [3, 'Great']], :
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>
坏的
好啊
伟大的
但是我希望标签有一个额外的类,比如:
<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>
伟大的
更新:此类在以下位置静态定义:这可以通过使用块来实现:
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
此文档可以展示一些其他示例:如果有人想知道如何在设置
布尔\u样式=:nested
时将类添加到标签包装单选按钮输入
调用输入时,您可以设置名为:item\u label\u class
的选项,例如:
<%= f.input :type, as: :radio_buttons,
collection: Listing::TYPES.map{ |type| [Listing.translate_type(type), type] },
label: false,
item_label_class: 'radio' %>
可以尝试添加
label_html:{class:'icon star'}
作为选项我尝试过但没有成功。事实上,我意识到这个源代码在最新版本的simple_form中发生了一些变化,现在正在寻找2.1而不是3.0.0.rc的源代码,以便更好地理解如何处理它。搜索了几个小时的答案。如果您想将类添加到输入单选按钮(我正在寻找),请使用以下命令:b.单选按钮(:class=>“收藏单选按钮图标星”)+b.标签(:class=>“收藏单选按钮图标星”)
这不再有效。它只会让你的标签完全消失。
def apply_default_collection_options!(options)
super(options)
options[:item_label_class] == 'radio' if input_type == :radio_buttons
end