Reputation: 361
I use simple_form in Rails, and I want to add different class for different radio input. I did some search, but no luck.
Here is a example:
Views
<%= f.label "Gift Pack?" %><br>
<%= f.input :gift_pack, as: :radio_buttons,
:collection => [[true, 'Gift Pack'], [false, 'Original Pack']],
label: false,
:label_method => :last,
:value_method => :first,
:input_html => { :class => 'gift_pack gift_pack_yes' } %>
Generated HTML
<input class="radio_buttons optional gift_pack gift_pack_yes" type="radio" value="true" name="book[gift_pack]" id="book_gift_pack_true"> Gift Pack
<input class="radio_buttons optional gift_pack gift_pack_yes" type="radio" value="false" name="book[gift_pack]" id="book_gift_pack_false"> Original Pack -->
Want to
HOW to make the second input's class is radio_buttons optional gift_pack gift_pack_no
, Notice the last class is gift_pack_no
.
Thanks.
Upvotes: 1
Views: 1696
Reputation: 3531
first create new view helper
def gift_radio_class(val)
val ? "gift_pack_yes" : " gift_pack_no"
end
in Your erb you have to call above helper
<%=f.collection_radio_buttons(:gift_val, [[true, 'Gift Pack'], [false, 'Original Pack']], :first,:second) do |b|%>
<%b.label{ b.radio_button(class: gift_radio_class(b.value)) + b.text }%>
<%end %>
we can retrieve class name based on boolen value of check box via view helper function gift_radio_class
check out docs if you need more custom solution http://www.rubydoc.info/github/plataformatec/simple_form/SimpleForm/FormBuilder:collection_radio_buttons
Upvotes: 2