radio_button 如何在数组中 return 取值?

How radio_button can return value in array?

例如有集合单选按钮:

= f.collection_radio_buttons :types, @languages, :itself, :itself, checked: ['java'],
    include_hidden: false, multiple: true do |b|
      .form-check-input
        = b.radio_button  
        = b.label

如果我selectradio_button,参数return'java'。但我需要 return 数组。例如 ['java']。

问题:

值数组可以return radio_buton吗?

Is it possible return radio_buton with value array?

是的。这是您需要的 html 类型:

<form method="post" action="/dave" name="languages">
  <%= hidden_field_tag :authenticity_token, form_authenticity_token %>
  <div><input type="radio" name="languages[]" value="java" />Java</div>
  <div><input type="radio" name="languages[]" value="ruby" />Ruby</div>
  <div><input type="radio" name="languages[]" value="python" />Python</div>
  <input type="submit" name="submit" />
</form>

这是我在参数中看到的:

Parameters: {
"authenticity_token"=>"<omitted>", 
"languages"=>["java"], 
"submit"=>"Submit"
}

我找到了解决方案:

.form-check
    %label
      = t('auth.users.form.roles_label')
    = f.collection_radio_buttons :languages, @languages, :itself, :itself, { checked: ['java'], include_hidden: false }, { multiple: true } do |b|
      .form-check-input
        = b.radio_button  
        = b.label