我对 SimpleForm 中的单选按钮有点问题。
当我使用
= f.association :manufactureType, :collection => ManufactureType.all, :as => :radio
Rails 只生成了几个单选按钮,但没有一个被选中。我希望默认选择第一个单选按钮。我怎样才能做到?
谢谢
我对 SimpleForm 中的单选按钮有点问题。
当我使用
= f.association :manufactureType, :collection => ManufactureType.all, :as => :radio
Rails 只生成了几个单选按钮,但没有一个被选中。我希望默认选择第一个单选按钮。我怎样才能做到?
谢谢
如果您将制造类型传递到视图中,您可以执行以下操作:
:checked => @manufacture_types[0]
或者
:checked => ManufactureType.first
我的例子稍微复杂一些,因为没有可供参考的集合或模型,其他答案都对我不起作用。
= f.input :attending, as: :radio_buttons, :collection => [ ['Yes', true], ['No', false] ], :checked => ['Yes', true]
根据 op 的评论,添加此参数对我有用:
:checked => 1
这是我的代码的摘录:
= f.input :body_format,
collection: [['markdown', 'Markdown']],
label_method: :last,
value_method: :first,
as: :radio_buttons,
checked: 'markdown', # THIS
required: true