Simple_form collection_radio_button and custom label class

I am trying to create a star rating form using a radio collection using FontAwesome, for this I really need to change the collection_radio_button input label classes created by simple_form, but cannot find any obvious solution.

So far I am using:

form_for @user do |f| f.collection_radio_buttons :rating, [[1, 'Bad'] ,[2, 'Ok'], [3, 'Great']], :first, :last, { item_wrapper_tag: false } end 

What generates:

 <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 would like the labels to have an extra class, for example:

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

UPDATE: this class is defined statically at: https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/tags.rb#L43

+6
source share
2 answers

This can be achieved using the 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 

This document can demonstrate another example: http://rubydoc.info/github/plataformatec/simple_form/SimpleForm/FormBuilder:collection_radio_buttons

+12
source

In case someone wonders how to add a class to a portable label label when you set boolean_style = :nested , here is what I came up with:

You can set an option called :item_label_class when invoking your input, fe:

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

I wanted to automate this stuff, so I defined a custom class CollectionRadioButtonsInput . You need to add the apply_default_collection_options! method apply_default_collection_options! :

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

Source: https://habr.com/ru/post/947647/


All Articles