simple_form的collection_radio_button和自定义标签类

我正在尝试使用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 

哪个产生:

       

但我希望标签有一个额外的类,如:

   

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

这可以通过使用块来实现:

 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 : collect_radio_buttons

如果有人想知道如何在设置boolean_style = :nested时将类添加到标签包装radiobutton输入boolean_style = :nested在这里是我带来的:

在调用输入时,您可以设置名为:item_label_class选项,fe:

 <%= f.input :type, as: :radio_buttons, collection: Listing::TYPES.map{ |type| [Listing.translate_type(type), type] }, label: false, item_label_class: 'radio' %> 

我想自动化这些东西,所以我定义了自定义的CollectionRadioButtonsInput类。 您需要添加方法apply_default_collection_options!

 def apply_default_collection_options!(options) super(options) options[:item_label_class] == 'radio' if input_type == :radio_buttons end