首先,我对Ruby很新,但发现自己处于支持和调整相当复杂的Web应用程序的位置。
我最近被要求更改所有形式的网址" https://hostname.com/opportunities/../ .."形式为" https://hostname.com/accounts/../ .."格式。我这样做只需更改routes.rb文件并用/ accounts /替换/ opportunities / routes并保持相同的控制器名称。这似乎符合预期目的。但是,有一些意想不到的后果,即仍然使用引用旧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)
答案 0 :(得分:2)
在redirect
方法中,您应该使用以下语法:
get '/stories/:name', to: redirect('/articles/%{name}')
参考: