加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – simple_form的collection_radio_button和自定义标签类

发布时间:2020-12-17 04:28:46 所属栏目:百科 来源:网络整理
导读:我正在尝试使用FontAwesome制作带有无线电收集的星级评级表,为此我实际上需要更改simple_form生成的collection_radio_button输入的标签类,但找不到任何明显的解决方案. 到目前为止我使用: form_for @user do |f| f.collection_radio_buttons :rating,[[1,'B
我正在尝试使用FontAwesome制作带有无线电收集的星级评级表,为此我实际上需要更改simple_form生成的collection_radio_button输入的标签类,但找不到任何明显的解决方案.

到目前为止我使用:

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>

更新:此类在静态定义:https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/tags.rb#L43

解决方法

这可以通过使用块来实现:
form_for @user do |f|
  f.collection_radio_buttons :rating,{ 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读