让我们采用以下路由器:
Rails.application.routes.draw do
scope ":locale", locale: /en|nl/ do
get "/:slug", to: "pages#prefixed_action", constraints: { slug: /.*/ }
end
root to: "pages#simple_action"
get "/:slug", to: "pages#simple_action", constraints: { slug: /.*/ }
end
如果请求指定了语言环境,作用域会捕获它并将其转发给 pages#prefixed_action
。如果不是,请求将转发到pages#simple_action
。
我的问题是,如果我在浏览器中输入 localhost:3000/en
,它不会被范围捕获。我该如何安排?
Edit1:我希望将请求路由到“pages#prefixed_action”
Edit2:在代码示例中添加了一个 root
声明
其中一种可能性是在范围内指定 root
:
scope ":locale", locale: /en|nl/ do
root 'pages#prefixed_action'
get "/:slug", to: "pages#prefixed_action", constraints: { slug: /.*/ }
end
我是一名优秀的程序员,十分优秀!