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

Rails routes with optional parameters priorities

我的路线文件中有一个奇怪的问题。 这是我需要了解的部分 这条路线行不通

  # 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 将引导至主页。

任何人都可以解释为什么这是必须发生的吗?

谢谢。

有这样的路线<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的值。