ruby-on-rails - no implicit conversion of Symbol into Integer rails 5.2.2.1
问题描述
I tried using retrofit2 to post to rails to filter some data from places
params[:filter] are are:
[
{
"id"=>2,
"name"=>"Cafe",
"service_code"=>0
},
{
"id"=>3,
"name"=>"restaurant",
"service_code"=>0
}
]
so I wanted to get the id, with a simple params[:filter][:id] but it returns
no implicit conversion of Symbol into Integer
thanks for the help and sorry if it seems like a simple problem
解决方案
no implicit conversion of Symbol into Integer (TypeError)
This is error is raised in this case because you're trying to access to the elements in an array as it'd be a hash. Ruby tries to convert the symbol you're passing as parameter to the []
method, because the receiver is an array, and arrays can be accessed to their elements by their index.
In short, you have an array of hashes, if you need the id from one of them, filter the elements until you get it and there you can use ['id']
:
[{ "id"=>2, "name"=>"Cafe", "service_code"=>0 },
{ "id"=>3, "name"=>"restaurant", "service_code"=>0 }][0]['id']
# 2
推荐阅读
- struct - 如何在 Julia 中为结构创建随机数据?
- javascript - 对 Discord Bot 使用 ffmpeg 静态而不是二进制文件
- javascript - 有没有办法强制在 Safari 中始终为 IOS 移动设备打开 Web 应用程序链接?
- unix - 如何将 sed 命令的输出用作字符串并在 grep 命令中使用该字符串?
- c# - 材料设计wpf c#中的MessageBox
- php - 根文件夹中的 laravel .htaccess
- android - 移动构建中缺少 Unity ScriptableObject
- angular - 重新加载 http://localhost:4200/home 时出现 404 错误
- nginx - nginx 错误“服务器”指令在这里不允许
- asterisk - 增加通道发起的超时