本文介绍了对于具有相同URI模式的路由,哪个首先匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设Rails应用程序绘制以下路线(即耙路将显示的路线):

Assume a Rails application draws the following routes (i.e. this is what rake routes would show):

Verb  URI Pattern  Controller#Action
GET   /            one#show
GET   /            two#show
GET   /            three#show

请求根路径时这些路由的匹配顺序是什么(即 / )?哪些因素决定了哪个路线最先匹配?

What is the matching order for these routes when requesting the root path (i.e. /)? What factors detemine which route is matched first?

背景:我面对的是我正在为其编写的Rails应用程序(话语)一个插件。 Rails应用程序在其route.rb文件中通过 root到:设置了许多根路由。我的插件尝试提供自己的根路由,如下所示:

Background: I’m faced with a Rails application (Discourse) for which I’m writing a plugin. The Rails application sets up a bunch of root routes via root to: in its routes.rb file. My plugin tries to supply its own root route like this:

Discourse::Application.routes.append do
  root to: 'custom#show'
end

…为从耙齿路线

Verb  URI Pattern  Controller#Action
GET   /            one#show
GET   /            two#show
GET   /            three#show
GET   /            customshow

我的问题是我无法使这个新的根优先于其他根路由。我尝试使用前置而不是附加,它将自定义根路由移至输出的顶部,但仍然

My problem is that I am not unable to make this new root take precedence over the other root routes. I tried using prepend instead of append which moves the custom root route to the top of the output, but it is still not matched first.

编辑:大概是按照指定的顺序匹配路由,无论 rake routes 输出。

Edit: Presumably, routes are matched in the order they’re specified in, no matter the order from the rake routes output.

推荐答案

第一个匹配的路由将是先前指定的路由在 routes.rb 中。

The first matched route will be the one specified earlier in routes.rb.

来源:,第二注

这篇关于对于具有相同URI模式的路由,哪个首先匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:23