title: Redirect Ruby Routes
body: <p>First thing first, I am fairly new to Ruby but find myself in the position of having to support and tweak a fairly complex web app.</p>
  
  <p>I was recently asked to change all the url's which were of a form "<a href="https://hostname.com/opportunities/../" rel="nofollow noreferrer">https://hostname.com/opportunities/../</a>.." to be of the form "<a href="https://hostname.com/accounts/../" rel="nofollow noreferrer">https://hostname.com/accounts/../</a>.." format.  I did this by simply changing the routes.rb file and replacing /opportunities/ routes with /accounts/ and keeping the same controller names.  This seems to have served the intended purpose.  However, there have been some unintended consequences, namely anyone still using links with reference to the old URLs is hit with a 404 error.</p>
  
  <p>I was thinking that I needed to redirect all the old routes to the new routes, but with the exception of the base route (i.e. <a href="https://hostname.com/opportunities" rel="nofollow noreferrer">https://hostname.com/opportunities</a>) all URLs with additional levels of detail fail to dynamically generate.  Instead the return is literally in the form "<a href="https:///accounts/:region/:state/:structure/:type/:id" rel="nofollow noreferrer">https:///accounts/:region/:state/:structure/:type/:id</a>"</p>
  
  <p>Please let me know what I need to do to accomplish the desired outcome.  Should I be using a wildcard in the redirect, or some other trick?  How can I get two sets of routes to use the same controllers (if in fact I have it correct that this is what I am needing?)</p>
  
  <pre><code>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)
  </code></pre>