简体   繁体   English

在Rails表单帮助器中从check_box转换为radio_button

[英]Convert from check_box to radio_button in Rails form helper

I have check_box inside my form like this below. 我的表单中有如下所示的check_box。 I can select or deselect an item. 我可以选择或取消选择一个项目。 But what I want is to select only one item from the group of options (items) that I have. 但是我想要的是从我拥有的选项(项目)组中仅选择一项。 How can I do that using radio_button ?? 我该如何使用radio_button?

      <%= f.fields_for :items, @form_items do |fields| %>
        <% if editable %>
            <%= fields.hidden_field :unit_id %>
            <%= fields.check_box :_destroy, { checked: item.persisted?, class: 'add-item', "data-price" => item.pre_tax_amount, readonly: true }, '0', '1' %>
        <% end %>

Thanks in advance. 提前致谢。

You should be able to use the collection_radio_button helper that rails provide using @form_items as the collection. 您应该能够使用rails使用@form_items作为集合提供的collection_radio_button帮助器

<%= fields.collection_radio_buttons :item, @form_items %>

This should give you a starting place; 这应该给您一个起点。 not knowing the contents of the collection makes it difficult to give a full example. 不知道集合的内容将很难举一个完整的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM