Rails 3路由具有多个可选参数

我正在尝试创建一个具有可选参数以及变化顺序的Rails路由。

这个问题描述了一个类似的问题: 具有多个,可选和漂亮参数的路由

我正在尝试创建其中包含地图filter的路线,例如参数但没有参数URL样式。 想法是让它们看起来像

/search/country/:country/ /search/country/:country/state/:state/ /search/country/:country/state/:state/loc/:lat/:long/ 

但你也应该能够搜索

 /search/state/:state/ /search/state/:state/country/:country/ /search/loc/:lat/:long/ 

我知道我可以使用路由通配来编写复杂的正则表达式语句 – 但是我想知道是否有一种方法可以使用未指定顺序的多个可选路由参数,类似于

 /search/( (/country/:country)(/state/:state)(/loc/:lat/:long) ) 

谢谢!

您可以使用lambda constraints来使用多个搜索选项:

  search_options = %w(country state loc) get('search/*path',:to => 'people#search', constraints: lambda do |request| extra_params = request.params[:path].split('/').each_slice(2).to_h request.params.merge! extra_params # if you want to add search options to params, you can also merge it with search hash (extra_params.keys - search_options).empty? end) 

您可以为更复杂的路线制作不同的lambda