Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top