Ruby-on-rails 在 Rails 中,如何处理多个选中的复选框,只是在 , 上拆分,还是?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4425176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Rails, how to handle multiple checked checkboxes, just split on the , or?
提问by Blankman
Curious what the 'rails way' of handling the situation when a user checks multiple checkboxes (with the same name value), and it gets posted back to the controller.
好奇当用户选中多个复选框(具有相同的名称值)时处理这种情况的“rails 方式”,并将其回发到控制器。
How would you check if multiple items were selected, then splitted on the ID values etc.
您将如何检查是否选择了多个项目,然后根据 ID 值等进行拆分。
回答by Matchu
The easiest way of doing this is to set those checkboxes up to become an array.
最简单的方法是将这些复选框设置为一个数组。
HTML:
HTML:
<input type="checkbox" name="tag_ids[]" value="1" />
<input type="checkbox" name="tag_ids[]" value="2" />
<input type="checkbox" name="tag_ids[]" value="3" />
Controller:
控制器:
tag_ids = params[:tag_ids]
(Of course, you'd probably be using form_for-based helpers in the view, and therefore mass-assigning the tag IDs. This is just the most generic example.)
(当然,您可能会form_for在视图中使用基于 -based 的助手,因此批量分配标签 ID。这只是最通用的示例。)
回答by FlyingV
f.check_box :tag_ids, {multiple: true}, 1, nil
Is the right answer:
正确答案是:
Here is the reason, there is a 'multiple: true' option that allows your input to be placed in an array. If there isn't a multiple: true option this will not be allowed.
这就是原因,有一个 'multiple: true' 选项允许您将输入放置在数组中。如果没有 multiple: true 选项,则不允许这样做。
回答by Nesha Zoric
Here is an example of view and controller for example where multiple cleaners can be in multiple cities.
这是一个视图和控制器的例子,例如多个清洁工可以在多个城市。
<%= form_for(@cleaner) do |f| %>
<p>
<%= f.label :cities %><br />
<% for city in City.all %>
<%= check_box_tag "cleaner[city_ids][]", city.id, @cleaner.cities.include?(city) %>
<%=h city.name %><br />
<% end %>
</p>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And in controller
并在控制器中
def cleaner_params
params.require(:cleaner).permit(city_ids: [])
end
You can find complete tutorial on "rails way" of doing this https://kolosek.com/rails-join-table/
您可以找到有关执行此操作的“rails 方式”的完整教程https://kolosek.com/rails-join-table/
回答by Roel4811
In addition to Chuck Callebs answer, I realised that with sending an empty string instead of nilor falseas unchecked value, Rails will understand to remove associated ids on an update action:
除了 Chuck Callebs 的回答之外,我意识到通过发送一个空字符串而不是nil或false作为未经检查的值,Rails 会理解在更新操作中删除关联的 id:
<%= f.check_box :tag_ids, {multiple: true}, tag.id, '' %>
<%= f.check_box :tag_ids, {multiple: true}, tag.id, '' %>
回答by user284130
If you want to use a checkedparam you have to write this :
如果你想使用一个checked参数,你必须这样写:
check_box_tag "tag_ids[]", 1, true
And not this :
而不是这个:
check_box_tag 'tag_ids[]', 1, true
It took me a while to figure out, I hope it will help someone.
我花了一段时间才弄明白,我希望它会帮助某人。

