首先,我对Ruby相当陌生,但我发现自己必须支持和调整一个相当复杂的web应用程序。
我最近被要求更改所有的url,这些url都是一个表单“
https://hostname.com/opportunities/../
..“形式”
https://hostname.com/accounts/../
..“格式。我只需更改routes.rb文件,将/opportunities/routes替换为/accounts/并保留相同的控制器名称即可。这似乎达到了预期目的。然而,也出现了一些意外后果,即任何仍在使用引用旧URL的链接的人都会遇到404错误。
我在想,我需要将所有旧路由重定向到新路由,但基本路由除外(即。
https://hostname.com/opportunities
)所有具有其他详细级别的URL都无法动态生成。取而代之的是,回报的形式是”
https:///accounts/:region/:state/:structure/:type/:id
“”
请让我知道我需要做什么来实现预期的结果。我应该在重定向中使用通配符还是其他一些技巧?如何获得两组路由以使用相同的控制器(如果事实上我正确地知道这是我所需要的?)
get '/accounts/:id', to: 'opportunities#show', constraints: {id: /\d+/}, as: 'opportunity'
get '/accounts/:region/:state/:structure/:type/:id', to: 'opportunities#show', constraints: {id: /\d+/}, as: 'long_opportunity'
get '/accounts/:region/:state/:term/:type/:id', to: 'opportunities#show', constraints: {id: /\d+/}, as: 'short_opportunity'
get '/accounts(/:region(/:state(/:structure(/:type))))', to: 'opportunities#index', as: 'opportunities'
post '/accounts/:id/consider', to: 'opportunities#possible', constraints: {id: /\d+/}, as: 'possible_opportunity'
get "/opportunities/", to: redirect("/accounts/", status: 302)
get '/opportunities/:id', to: redirect('/accounts/:id', status: 302)
get '/opportunities/:region/:state/:structure/:type/:id', to: redirect('/accounts/:region/:state/:structure/:type/:id', status: 302)
get '/opportunities/:region/:state/:term/:type/:id', to: redirect('/accounts/:region/:state/:term/:type/:id', status: 302)
get '/opportunities(/:region(/:state(/:structure(/:type))))', to: redirect('/accounts(/:region(/:state(/:structure(/:type))))', status: 302)