我有一个允许输入多个ID的表单,例如:
<input name="the_ids[]" type="text" placeholder="Enter an ID" />
<input name="the_ids[]" type="text" placeholder="Enter an ID" />
<input name="the_ids[]" type="text" placeholder="Enter an ID" />
当表单提交时,我得到的参数传递为:
"the_ids"=>["1, 2, 3"],
在控制器中,我尝试以以下方式访问它们:
params[:the_ids[]].each do |id|
do_something_with(id)
end
我不断收到错误:
wrong number of arguments (0 for 1..2)
> params[:the_ids[]].each do |id|
跟踪显示以下内容:
app/controllers/the_controller.rb:10:in `[]'
我无法弄清楚。我已经查看了其他一些文章,并尝试使用其他语法进行访问,例如:
params["the_ids[]"]
但这产生了一个错误,指出这是一个nil类。
请告知是否需要更多详细信息。
如您所见"the_ids"=>["1, 2, 3"]
,参数名称为the_ids
,您可以使用进行访问params[:the_ids]
。
params[:the_ids].each do |id|
do_something_with(id)
end
仅在客户端需要使用the_ids[]
inname="the_ids[]"
才能告知Railsthe_ids
将其作为数组参数读取。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句