首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有可选参数优先级的Rails路由

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

Stack Overflow用户
提问于 2015-02-08 10:46:05
回答 1查看 253关注 0票数 0

我的路线文件里有个奇怪的问题。这是我需要明白的部分,这条路不起作用

代码语言:javascript
复制
  # 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

但这份工作

代码语言:javascript
复制
#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

如果我使主页路由比其他路由具有更低的优先级,那么它可以工作,但是如果它与其他路由一样位于顶部: match '(/:locale)/search_amazon‘=> 'v3/products#search_amazon',:=> V3Constraint将导致主页。

有谁能解释一下为什么要发生这种事吗?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-09 12:12:42

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

代码语言:javascript
复制
match '(/:locale)/search_amazon' => 'v3/products#search_amazon', :constraints => V3Constraint

在本例中,它将匹配,因为locale在这里是可选的。

代码语言:javascript
复制
match '/:locale' => 'v3/home#index', :constraints => V3Constraint, :as => :home

在这里,它将匹配使search_amazon作为locale的值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28393046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档