让我们选择以下路由器:
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:我希望将请求路由到"页面#prefixed_action"
Edit2:在代码示例中添加了root
声明
答案 0 :(得分:3)
其中一种可能性是在范围内指定root
:
scope ":locale", locale: /en|nl/ do
root 'pages#prefixed_action'
get "/:slug", to: "pages#prefixed_action", constraints: { slug: /.*/ }
end
答案 1 :(得分:1)
也许只是这样:
get '/:locale', to: "pages#prefixed_action", constraints: { locale: /(en|nl)/ }
get '/:locale', to: "pages#simple_action", constraints: { locale: /.*/ }
如果您的目标是将pages#prefixed_action
和/en
以及所有其他路径/nl
发送至pages#simple_action
。如果是,请填写
get '/:locale', to: "pages#simple_action", constraints: { locale: /.*/ }
作为路线中的最后一条路线