Ruby / Rails / v5 - Routing Order - Root - root mater 的位置与路由文件的顺序有关吗?
Ruby / Rails / v5 - Routing Order - Root - Does the location of root mater in regards to the order on routes file?
Ruby / Rails / v5 - 路由顺序 - 根
路由文件的顺序重要吗?
比如我的"root"需要放在路由文件的顶部还是底部?
routes.rb
Rails.application.routes.draw do
#Static Pages
get 'static_pages/main'
get 'static_pages/about'
get 'static_pages/contact'
get 'static_pages/help'
# Root
root 'static_pages#landing'
end
Does the order of my routes file matter?
是的,在某些情况下,您在 url 中使用变量。
假设您有一个这样的路由文件:
get ':page', to: "ages#page"
get 'about', to: "pages#about"
第二个永远不会被命中,因为第一个正在捕获参数 page
中的字符串 about
。
写root 'static_pages#landing'
和get '/', to: "static_pages#landing"
是一样的。由于这与没有变量的 精确字符串 (/
) 匹配,因此它不会干扰您的其他路线。
Ruby / Rails / v5 - 路由顺序 - 根
路由文件的顺序重要吗?
比如我的"root"需要放在路由文件的顶部还是底部?
routes.rb
Rails.application.routes.draw do
#Static Pages
get 'static_pages/main'
get 'static_pages/about'
get 'static_pages/contact'
get 'static_pages/help'
# Root
root 'static_pages#landing'
end
Does the order of my routes file matter?
是的,在某些情况下,您在 url 中使用变量。
假设您有一个这样的路由文件:
get ':page', to: "ages#page"
get 'about', to: "pages#about"
第二个永远不会被命中,因为第一个正在捕获参数 page
中的字符串 about
。
写root 'static_pages#landing'
和get '/', to: "static_pages#landing"
是一样的。由于这与没有变量的 精确字符串 (/
) 匹配,因此它不会干扰您的其他路线。