我在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
这些规则本身就有效,但问题在于它们互相干扰。
目前第一条规则没有得到应用,因为第二条规则正在捕获这些请求,尽管我试图将第二条规则限制为仅捕获包含+
符号的辅助部分的网址。
如何构建第二条规则,使其仅捕获包含两段网址的请求(第二段必须包含加号)?
答案 0 :(得分:0)
另一个选择是确保你有一个城市,然后是更多:
get ":topic/:city+:multi_cities" => "controller#action", :multi_cities => /\w[\+]?[\w]?/
然后,您可以在控制器中解析params[:multi_cities]
,以便进一步区分城市。