在routes.rb中捕获包含特定字符的URL

时间:2013-11-13 01:01:27

标签: ruby-on-rails ruby ruby-on-rails-3.2 routes url-routing

我在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

这些规则本身就有效,但问题在于它们互相干扰。

目前第一条规则没有得到应用,因​​为第二条规则正在捕获这些请求,尽管我试图将第二条规则限制为仅捕获包含+符号的辅助部分的网址。

如何构建第二条规则,使其仅捕获包含两段网址的请求(第二段必须包含加号)?

1 个答案:

答案 0 :(得分:0)

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

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

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