Rails - Parameter with multiple values in the URL when consuming an API via Active Resource

前端 未结 3 1006
無奈伤痛
無奈伤痛 2021-02-07 08:09

I am consuming an API that expects me to do requests in the following format:

?filter=value1&filter=value2

However, I am using Active Resou

3条回答
  •  囚心锁ツ
    2021-02-07 08:52

    You can generate query strings containing repeated parameters by making a hash that allows duplicate keys. Because this relies on using object IDs as the hash key, you need to use strings instead of symbols, at least for the repeated keys.

    (reference: Ruby Hash with duplicate keys?)

    params = { consumer_id: 1 } # replace with self.id
    params.compare_by_identity
    params["filter"] = "value1"
    params["filter"] = "value2"
    params.to_query #=> "consumer_id=1&filter=value1&filter=value2"
    

提交回复
热议问题