Change expression_options for radio buttons

Hi, I am 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'

like that:

.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 does not actually pass the value of the switch. Also, after it is sent, by default it does not return to selection if it should remain on the selected switch.

+4
source share
1 answer

You can use the radio_button_taghelper and set the parameter correctly checked.

Using your example, it is 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
+1
source

Source: https://habr.com/ru/post/1527068/


All Articles