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

前端 未结 3 1016
無奈伤痛
無奈伤痛 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:42

    http://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters

    Try :filter[] => value, :filter[] => value2

    0 讨论(0)
  • 2021-02-07 08:51

    to create a valid query string, you can use

    params = {a: 1, b: [1,2]}.to_query

    http://apidock.com/rails/Hash/to_query
    http://apidock.com/rails/Hash/to_param

    0 讨论(0)
  • 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"
    
    0 讨论(0)
提交回复
热议问题