简体   繁体   English

获取所有选中的复选框值

[英]Get all checked checkboxes values

i have the following form: 我有以下形式:

<%= form_for([company,service], :remote => true) do |f| %>
    <%= health_care_insurances.each do |hc_insurance| %>
        <label><%= hc_insurance.name %></label>
        <input id="health_care_insurance_<%= user.id %>" name="<%= hc_insurance.id %>" value="<%= hc_insurance.id %>" type="checkbox"><br/>
    <% end %>
    ...
<% end %>

When it's submited, and for the sake of good performance, i would like to get an array with all "hc_insurance.ids" that are checked. 提交后,为了获得良好的性能,我想获取一个包含所有已检查的“ hc_insurance.ids”的数组。 I know i might use javascript to add them to a hidden_field and then pick them up from there, but im not sure that's a good way to go. 我知道我可能会使用javascript将它们添加到hidden_​​field,然后从那里拾取它们,但是我不确定这是个好方法。

Is there any rails way to pick them up from the controller? 是否有任何方法可以从控制器中拾取它们?

Make all the name of the checkboxes the same with array box, like below (this would submit only the checked values to the backend) 使复选框的所有名称与数组框相同,如下所示(这只会将选中的值提交到后端)

<input id="health_care_insurance_<%= user.id %>" name="health_care_insurence_ids[]" value="<%= hc_insurance.id %>" type="checkbox"><br/>

To get the id of all the health_care_insurence in controller (assuming health_care_insurence is a single model). 要获取控制器中所有health_care_insurence的ID(假设health_care_insurence是单个模型)。

HealthCareInsurence.pluck(:id)

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

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