将options_for_select更改为单选按钮

时间:2014-02-15 07:57:51

标签: ruby-on-rails twitter-bootstrap twitter-bootstrap-3 haml

您好我正在尝试更改以下内容:

  .form-group
    = label_tag :metric, 'Metric', class: 'sr-only'
    = select_tag :metric, options_for_select(Report::METRICS, selected: @report.metric), class: 'form-control'

类似于以下内容:

.form-group.pull-left
        = label_tag :metric, 'Metric', class: 'sr-only'
        .btn-group{"data-toggle" => "buttons"}
          %label.btn.btn-default.active
            %input#option1{name: "options", type: "radio", value: "value1"}/
            Value1
          %label.btn.btn-default
            %input#option2{name: "options", type: "radio", value: "value2"}/
            Value2

但问题是,当我提交表单时,它实际上并没有提交单选按钮值。在提交之后,当它应保留在所选单选按钮上时,它默认为无选择。

1 个答案:

答案 0 :(得分:1)

您可以使用radio_button_tag帮助程序并正确设置checked参数。

使用您的示例,它是这样的:

.form-group.pull-left
  = label_tag :metric, 'Metric', class: 'sr-only'
    .btn-group{"data-toggle" => "buttons"}
      %label.btn.btn-default.active
        = radio_button_tag('options', 'value1', params[:options] == 'value1')
        Value1
      %label.btn.btn-default
        = radio_button_tag('options', 'value2', params[:options] == 'value2')
        Value2