如何301将旧URL重定向到嵌套资源

时间:2014-05-22 02:34:25

标签: ruby-on-rails routing

问题在于:

当有人输入domain.com/branches/10

我需要重定向到domain.com/businesses/1/branches/10

新路线

  resources :businesses do
    resources :branches
  end

旧路线

  match 'branches/:id', to: 'branches#show'

谢谢!

1 个答案:

答案 0 :(得分:0)

好的,我明白了:

match '/branches(/*path)' => redirect{ |params| "/businesses/#{Branch.find(params[:path]).business.to_param}/branches/#{Branch.find(params[:path]).to_param}" }

我还需要添加.to_param,因为我最近也切换到半友好网址(id + name)。这适用于将id或友好URL转换为新的嵌套路径。