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

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

问题描述

很好奇当用户选中多个复选框(具有相同的名称值)时处理这种情况的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天全站免登陆