문제

Hi i'm trying to change the following:

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

to something like the following:

.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

However the problem is that when I submit the form, it doesn't actually submit the radio button value. Also after it submits, it defaults back to no selection when it should remain on the selected radio button.

도움이 되었습니까?

해결책

You can use the radio_button_tag helper and set the checked parameter correctly.

Using your example, it's something like this:

.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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top