首页 > 解决方案 > 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

标签: ruby-on-rails

解决方案


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

推荐阅读