rails,simple_form,如何在radiobuttons中设置默认选定项

时间:2012-06-17 10:00:20

标签: ruby-on-rails simple-form

如何将'true'设为默认值?

已选择:true 选项无效

<%= f.input :smoke, label: "Smoke", :collection => [[true, "You can"], [false, "You can't"]],
         label_method: :last, value_method: :first, as: :radio_buttons, selected: true %>

1 个答案:

答案 0 :(得分:4)

您是否有任何理由避免在模型或控制器中设置默认值?也许可以使用simple_form在视图级别将'true'设置为默认值,但之后您必须为新的/编辑操作创建两个单独的表单,或者为视图代码添加额外的逻辑(以正确显示'default'true或actual值)。

否则,向控制器添加单行可能可以解决问题,我没有看到任何副作用:

def new
  @model = Model.new
  @model.smoke = true # 'true' will be default
  ...