提交班级名单。一次添加3名学生。每个学生都有最初、最后的年龄。
问题:我们如何才能将所有学生放入数组中?
students[0] => Array (
["first"] => "first name for 0",
["last"] => "last name for 0",
["age"] => "age for 0"
),
students[1] => Array (
["first"] => "first name for 1",
["last"] => "last name for 1",
["age"] => "age for 1"
),
...
Details
对于一名学生:
<input type="text" name="first">
<input type="text" name="last">
<input type="text" name="age">
我们可以在单独的数组中返回多个学生,如下所示:
<input type="text" name="students[first][]">
<input type="text" name="students[last][]">
<input type="text" name="students[age][]">
它返回一个包含第一个、最后一个和年龄的数组
students["first"] = [array of first names]
students["last"] = [array of last names]
students["age"] = [array of ages]
理论上,我们可以通过访问相同的索引(每个数组为“3”)来获取学生的所有信息。
我们不想以编程方式在表单中添加索引。
不要:
<input type="text" name="students[hardcoded_index][first]">
<input type="text" name="students[hardcoded_index][last]">
<input type="text" name="students[hardcoded_index][age]">
如果出于某种原因,我们可以使用 Rails 来查看视图,但也可以使用表单助手或 HTML。