Rails 路由:将路由匹配放入函数中以降低复杂性
Rails routing: putting matches for routing in a function to reduce complexity
我有一个 rails 应用程序,它执行一些前期范围界定以处理多个子域和多种语言,但结果有两组相同的内部匹配。我想将这些内部匹配分解为一个函数,以便我可以减少和重用代码我在路线中找不到任何人这样做的例子。
代码:
constraints lambda { |request| request.subdomains[0].include? "internal" } do
scope ":hash" do
get '/', to: 'product#index'
get '/:productID', to: 'product#show'
end
get "", to: 'product#no_hash'
get "/*path", to: 'product#no_hash'
end
constraints lambda { |request| !request.subdomains[0].include? "internal" } do
scope ":hash" do
get '/', to: 'product#index'
get '/:productID', to: 'product#show'
end
end
同样,我的目标是将内部匹配放入一个函数中,这样我就可以减少重复、重用代码并拯救世界。请帮忙。
嗯,我有两个答案:
第一个:实际上你的共享路由不依赖于你的子域,所以你可以将它们写在你的约束之外,它们将被直接共享。
另一个,有一个函数
def shared_routes
scope ":hash" do
get '/', to: 'product#index'
get '/:productID', to: 'product#show'
end
end
constraints lambda { |request| request.subdomains[0].include? "internal" } do
shared_routes
get "", to: 'product#no_hash'
get "/*path", to: 'product#no_hash'
end
constraints lambda { |request| !request.subdomains[0].include? "internal" } do
shared_routes
end
虽然我不确定我能否帮助你拯救世界:)
我有一个 rails 应用程序,它执行一些前期范围界定以处理多个子域和多种语言,但结果有两组相同的内部匹配。我想将这些内部匹配分解为一个函数,以便我可以减少和重用代码我在路线中找不到任何人这样做的例子。
代码:
constraints lambda { |request| request.subdomains[0].include? "internal" } do
scope ":hash" do
get '/', to: 'product#index'
get '/:productID', to: 'product#show'
end
get "", to: 'product#no_hash'
get "/*path", to: 'product#no_hash'
end
constraints lambda { |request| !request.subdomains[0].include? "internal" } do
scope ":hash" do
get '/', to: 'product#index'
get '/:productID', to: 'product#show'
end
end
同样,我的目标是将内部匹配放入一个函数中,这样我就可以减少重复、重用代码并拯救世界。请帮忙。
嗯,我有两个答案:
第一个:实际上你的共享路由不依赖于你的子域,所以你可以将它们写在你的约束之外,它们将被直接共享。
另一个,有一个函数
def shared_routes
scope ":hash" do
get '/', to: 'product#index'
get '/:productID', to: 'product#show'
end
end
constraints lambda { |request| request.subdomains[0].include? "internal" } do
shared_routes
get "", to: 'product#no_hash'
get "/*path", to: 'product#no_hash'
end
constraints lambda { |request| !request.subdomains[0].include? "internal" } do
shared_routes
end
虽然我不确定我能否帮助你拯救世界:)