Rails路由具有可选参数优先级

时间:2015-02-08 10:46:06

标签: ruby-on-rails ruby-on-rails-3 routing routes rails-routing

我的路线文件中有一个奇怪的问题。 这是我需要理解的部分 这条路线不起作用

  # V3
  # V3 - Home Page
  match '/:locale' => 'v3/home#index', :constraints => V3Constraint, :as => :home
  # V3 - Search
  match '(/:locale)/products/search' => 'v3/products#search', :constraints => V3Constraint
  # V3 - Categories index
  match '(/:locale)/categories/(:parent_category((/*path)/:category))/(:full)' => 'v3/products#index', :constraints => V3Constraint, :as => :category
  # V3 - Prduct Page
  match '/:locale/products/:product' => 'v3/products#show', :constraints => V3Constraint, :as => :product
  match '(/:locale)/search_amazon' => 'v3/products#search_amazon', :constraints => V3Constraint
  # EOF V3

但是这项工作

#V3 - Search
  match '(/:locale)/products/search' => 'v3/products#search', :constraints => V3Constraint
  # V3 - Categories index
  match '(/:locale)/categories/(:parent_category((/*path)/:category))/(:full)' => 'v3/products#index', :constraints => V3Constraint, :as => :category
  # V3 - Product Page
  match '/:locale/products/:product' => 'v3/products#show', :constraints => V3Constraint, :as => :product
  match '(/:locale)/search_amazon' => 'v3/products#search_amazon', :constraints => V3Constraint
  # V3 - Home Page
  match '/:locale' => 'v3/home#index', :constraints => V3Constraint, :as => :home

如果我使主页路由的优先级低于其他工作路由器的优先级,但是如果它与其他路由器一样在顶部 这条路线:      匹配'(/:locale)/ search_amazon' => ' v3 / products#search_amazon',:constraints => V3Constraint 将进入主页。

请解释为什么会发生这种情况?

感谢。

1 个答案:

答案 0 :(得分:1)

拥有这样的路线<yourdomain>/search_amazon将匹配这两条路线中的第一条

match '(/:locale)/search_amazon' => 'v3/products#search_amazon', :constraints => V3Constraint

在这种情况下,它会匹配,因为此处locale是可选的。

match '/:locale' => 'v3/home#index', :constraints => V3Constraint, :as => :home

在此处,它会将search_amazon作为locale的值。