GET /bids?countries=us,uk&categories=finance,sports&channels=ca,ga
期望响应为:
{
"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 }
]
}
如何为这个url制作路由请帮助我-任何有经验的人都知道我在做什么制作路由?提前谢谢。
这只是一个普通的旧索引路由,带有一些用于过滤资源的额外查询字符串参数。你实际上不需要做任何特别的事情。
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
当用路由匹配请求URI时,Rails通常会忽略查询字符串参数,除非它们对应于已定义路由中的命名占位符-例如GET /bids?id=1
将匹配GET /bids/:id
。
查询字符串参数存在于params
对象中。就像从URI中的占位符中提取参数,并以相同的方式解析为正文中的格式数据一样。