2015-02-08 81 views
0

我的路由文件中有一個奇怪的問題。 這是我需要了解 此路線行不通具有可選參數優先級的Rails路由

# 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

有這樣一個<yourdomain>/search_amazon路由將匹配這兩種路線的第一個

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

在這種情況下,它會匹配,因爲locale是可選在這裏。

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

雖然這裏將媒人search_amazonlocale值。