gpt4 book ai didi

ruby-on-rails - rails : Catch a request in a scope when the request doesn't match anything in the scope

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:32 26 4
gpt4 key购买 nike

让我们采用以下路由器:

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

关于ruby-on-rails - rails : Catch a request in a scope when the request doesn't match anything in the scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47696351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com