使用简单形式将唯一的 class 分配给各个无线电输入

Assigning a unique class to individual radio inputs using Simple Form

我有一对收音机输入,我想单独添加独特的 class。我知道使用 input_html {class: "example-class"} 但我需要两个 class 不同。例如:

<input type="radio" class="male" value="male">
<input type="radio" class="female" value="female">

我已经通读了 Simple Form 的文档和 SO 上的多篇文章,但似乎仍然找不到解决方案。相反,答案只会导致将一个基本 class 分配给其子输入的标签 and/or。目前我的单选按钮是这样设置的:

<%= f.collection_radio_buttons :gender, [['male', 'male'] ,['female', 'female']], :first, :last %> 

你可以像这样传递一个方块:

<%= f.collection_radio_buttons :gender, [['male', 'male'] ,['female', 'female']], :first, :last do |b|
      b.label { b.radio_button(class: (b.value == 'male' ? 'male' : 'female' )) }
    end
%> 

阅读本方法的documentation

.. It is also possible to customize the way the elements will be shown by giving a block to the method: