Ruby on rails 路由约束don';t为Rails参数正确赋值;使用'+';界定价值

Ruby on rails 路由约束don';t为Rails参数正确赋值;使用'+';界定价值,ruby-on-rails,regex,ruby-on-rails-4,routing,Ruby On Rails,Regex,Ruby On Rails 4,Routing,我希望能够支持国家代码和地区代码在我的申请的路线。例如: /实体/美国 /实体/美国+加拿大 /实体/us/mn /实体/美国/mn+wi+ia /实体/us+ca/bc+wa 我目前的路线: get "/entities/:country_code/(:region_code)" => "entities#index", :constraints => {:country_code=>/[a-zA-Z]{2}[\+\,]?/, :region_code=>/[a

我希望能够支持国家代码和地区代码在我的申请的路线。例如:

  • /实体/美国
  • /实体/美国+加拿大
  • /实体/us/mn
  • /实体/美国/mn+wi+ia
  • /实体/us+ca/bc+wa
我目前的路线:

  get "/entities/:country_code/(:region_code)" => "entities#index", :constraints => {:country_code=>/[a-zA-Z]{2}[\+\,]?/, :region_code=>/[a-zA-Z]{2}[\+\,]?/}
  resources :entities
尝试
/entities/us+ca
会导致此异常:

# Use callbacks to share common setup or constraints between actions.
def set_entity
  @entity = Entity.find(params[:id])
end 

Application Trace | Framework Trace | Full Trace

app/controllers/entities_controller.rb:79:in `set_entity'

Request

Parameters:

{"id"=>"us+ca"}
我将路线改为:

get "/entities/:country_code/(:region_code)" => "entities#index"
resources :entities
这允许多国家和地区查询工作(即
us+ca
被分配给
:country\u code
参数),但这打破了
/entities/new
路径--
new
现在被认为是
:country\u code
参数

我假设这个问题与正则表达式有关


有一个正则表达式可以满足我的需要吗?

你认为这样行吗

get '/entities/*country_code/*region_code', to: 'entities#index', constraints: { country_code: /[a-zA-Z]{2}[\+\,]?/, region_code: /[a-zA-Z]{2}[\+\,]?/ }

可能需要使用您的约束正则表达式。

我认为您的正则表达式不太正确。这里的一个将匹配2个字符(可选),后跟
+
。您还需要允许后续的字符对


尝试此正则表达式:
/[a-zA-Z]{2}(\+[a-zA-Z]{2})*/
(匹配2个字符,后跟0个或更多的
+
序列,后跟2个字符)。

不起作用;值(mn+wi)与
:id
参数相关联。我最近的尝试是
([a-zA-Z]{2}\+)?[a-zA-Z]{2}
,它对成对(例如
/entities/us+ca/mn+bc
)有效,但对更多(例如
/entititities/us+ca+nl
)无效。非常感谢你的帮助。