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.

Était-ce utile?

La 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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top