【问题标题】:Rails routes with optional parameters priorities带有可选参数优先级的 Rails 路线
【发布时间】:2023-03-08 19:44:02
【问题描述】:

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

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

谁能解释一下为什么会这样?

谢谢。

【问题讨论】:

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


    【解决方案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 的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      相关资源
      最近更新 更多