Rails – SimpleForm:集合中默认选择的单选按钮
我在SimpleForm中遇到了radiobuttons的问题。
当我使用
= f.association :manufactureType, :collection => ManufactureType.all, :as => :radio
Rails只生成很少的radiobuttons,但没有选择它们。 我希望默认选择第一个radiobutton。 我该怎么做?
谢谢
如果将制造类型传递到视图中,则可以执行以下操作:
: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