16

我对 SimpleForm 中的单选按钮有点问题。

当我使用

= f.association :manufactureType, :collection => ManufactureType.all, :as => :radio

Rails 只生成了几个单选按钮,但没有一个被选中。我希望默认选择第一个单选按钮。我怎样才能做到?

谢谢

4

4 回答 4

45

如果您将制造类型传递到视图中,您可以执行以下操作:

:checked => @manufacture_types[0]

或者

:checked => ManufactureType.first
于 2012-07-11T08:49:30.257 回答
12

我的例子稍微复杂一些,因为没有可供参考的集合或模型,其他答案都对我不起作用。

= f.input :attending, as: :radio_buttons, :collection => [ ['Yes', true], ['No', false] ], :checked => ['Yes', true]
于 2014-03-26T07:04:34.213 回答
5

根据 op 的评论,添加此参数对我有用:

:checked => 1
于 2013-03-12T19:43:58.910 回答
4

这是我的代码的摘录:

= f.input :body_format,
  collection: [['markdown', 'Markdown']],
  label_method: :last,
  value_method: :first,
  as: :radio_buttons,
  checked: 'markdown', # THIS
  required: true
于 2016-01-26T08:46:12.973 回答