1

我有三个规则routes.rb

match ":topic", to: 'topics#show', via: :get, as: :topic    

match ":topic/:multi_city(/:year(/:month))", to: "multi_cities#show", via: :get, as: :multi, constraints: {multi_city: /.*\+.*/}

scope ":topic" do
  scope ":city" do
    match "organizers", to: 'organizers#index', via: :get, as: :topic_organizers
    match "(:year(/:month))", to: "communities#show",   via: :get, as: :community
  end
end

目的是按顺序涵盖以下三种情况:

/football/  # only the topic  
/football/chicago+seattle+portland/  # a topic and a combination of cities  
/football/chicago # a topic and a single city

规则自己起作用,但问题是它们相互干扰。

目前第一条规则没有得到应用,因​​为第二条规则正在捕获这些请求,尽管我试图限制第二条规则只捕获带有包含+标志的次要部分的网址。

如何构造第二条规则,使其仅捕获包含具有两个段的 URL 的请求(并且第二段必须包含加号)?

4

1 回答 1

0

另一种选择是确保您拥有一个城市,然后是更多:

get ":topic/:city+:multi_cities" => "controller#action", :multi_cities => /\w[\+]?[\w]?/

然后,您可以在控制器中进行解析params[:multi_cities],以进一步将城市分开。

于 2013-11-13T01:52:42.450 回答