simple_form 集合单选按钮

simple_form Collection Radio Button

我有一个表单,其中这些单选按钮在两个表单之间重复:

= f.collection_radio_buttons :type, [['cardio', 'Cardio'], ['weights', 'Weights']], :first, :last, :item_wrapper_class => 'radio-inline'

有没有办法在单选按钮上添加 class,这样可以通过 jquery 轻松区分它们,你会如何使用 haml 来实现,就好像我附加在 ID 上一样, 数据库上的值被改变了?

集合单选按钮,例如:

form_for @user do |f|
  f.collection_radio_buttons :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
end

结果

<input id="user_options_true" name="user[options]" type="radio" value="true" />
<label class="collection_radio_buttons" for="user_options_true">Yes</label>
<input id="user_options_false" name="user[options]" type="radio" value="false" />
<label class="collection_radio_buttons" for="user_options_false">No</label>

https://github.com/plataformatec/simple_form#collection-radio-buttons

因此您应该尝试覆盖输入中生成的 CSS class 'collection_radio_buttons' 和 'ID'。