简体   繁体   English

在Rails中,如何从数组中挑选一个属于params hash的元素?

[英]In Rails, how to pick an element from an array that is part of a params hash?

An element from my params hash is an array by itself, like: 我的params哈希中的一个元素本身就是一个数组,例如:

Parameters:

{"utf8"=>"✓",
"scan"=>{"scores"=>"66 73 75 47"},
 "commit"=>"report",
 "id"=>"6"}

I've searched and I've tried, but I can't find the right params[....] syntax to access the integers in the "scores"-array. 我已经搜索并尝试过,但是我找不到正确的params[....]语法来访问“分数”数组中的整数。 I want to use them in my controller. 我想在控制器中使用它们。 )-: Help? )-: 救命?

That's not an array, that's a space-separated list in a single string. 那不是数组,而是单个字符串中用空格分隔的列表。 Access it like this: 像这样访问它:

params[:scan][:scores].split.map(&:to_i)

(I'm assuming you want integers, not strings.) (我假设您要的是整数,而不是字符串。)

Alternatively, if your code is making the request, you may be able to fix it to make the request with an array instead of a space-separated string. 另外,如果您的代码正在发出请求,则可以修复该问题,以便使用数组而不是用空格分隔的字符串来发出请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM