在Rails中,如何处理多个选中的复选框,只是拆分,或? [英] In Rails, how to handle multiple checked checkboxes, just split on the , or?

查看:148
本文介绍了在Rails中,如何处理多个选中的复选框,只是拆分,或?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户检查多个复选框(具有相同的名称值)时,处理这种情况的轨道方式很奇怪,并将其发回控制器。

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.

如何检查是否选择了多个项目,然后对ID值等进行拆分。

How would you check if multiple items were selected, then splitted on the ID values etc.

推荐答案

这是将这些复选框设置为一个数组。

The easiest way of doing this is to set those checkboxes up to become an array.

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" />

控制器:

tag_ids = params[:tag_ids]

(当然,在视图中使用 form_for 的助手,因此批量分配标签ID,这只是最通用的例子。)

(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.)

这篇关于在Rails中,如何处理多个选中的复选框,只是拆分,或?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆