Home > Mobile >  How to make routes for this URL?
How to make routes for this URL?

Time:12-20

GET /bids?countries=us,uk&categories=finance,sports&channels=ca,ga

And the expected response is:

{
  "bids": [
    { 'country': 'us', 'category': 'finance', 'channel': 'ca', 'amount': 4.0 },
    { 'country': 'us', 'category': 'finance', 'channel': '2ga', 'amount': 2.0 },
    { 'country': 'us', 'category': 'sports', 'channel': 'ca', 'amount': 2.0 },
    { 'country': 'us', 'category': 'sports', 'channel': 'ga', 'amount': 2.0 },
    { 'country': 'uk', 'category': 'finance', 'channel': 'ca', 'amount': 1.0 },
    { 'country': 'uk', 'category': 'finance', 'channel': 'ga', 'amount': 1.0 },
    { 'country': 'uk', 'category': 'sports', 'channel': 'ca', 'amount': 3.0 },
    { 'country': 'uk', 'category': 'sports', 'channel': 'ga', 'amount': 3.0 }
  ]
}

How to make routes for this url please help me out - any more experienced people know what I am doing for making routes ?. thanks in advance.

CodePudding user response:

That's just a plain old index route with some extra query string parameters to filter the resource. You don't actually need to do anything special.

resources :bids, only: [:index]
class BidsController < ApplicationController
  # GET /bids
  # GET /bids?countries=us,uk&categories=finance,sports&channels=ca,ga
  def index
    @bids = Bid.all
    # @todo implement filters
  end
end 

When matching a request URI with a route Rails will typically ignore the query string parameters unless they correspond to named placeholders in a defined route - GET /bids?id=1 for example will match GET /bids/:id.

Query string parameters are available in the params object. Just like parameters extraced from placeholders in the URI and are parsed identically to formdata in the body.

  • Related