为资源指定“子目录”-path

时间:2016-03-26 20:59:03

标签: ruby-on-rails rails-routing ruby-on-rails-5

我想为Team提供以下资源丰富的路线:

/admin/users/teams
/admin/users/team/new
/admin/users/team/:id
etc...

使用以下路线助手

admin_teams_path
new_admin_teams_path
edit_admin_teams_path
etc...

但是我知道并使用

唯一的命名空间
namespace :admin do
  namespace :users do
    resources :teams, except: [:index]
  end
end

我只是在这些长途路线助手中加入了“用户”:

admin_users_teams_path
new_admin_users_teams_path
edit_admin_users_teams_path

1 个答案:

答案 0 :(得分:2)

删除users命名空间并使用路由中的path选项:

namespace :admin, path: "admin/users" do
  resources :teams, except: [:index]
end

给出以下路线:

$ rake routes
    admin_teams POST   /admin/users/teams(.:format)          admin/teams#create
 new_admin_team GET    /admin/users/teams/new(.:format)      admin/teams#new
edit_admin_team GET    /admin/users/teams/:id/edit(.:format) admin/teams#edit
     admin_team GET    /admin/users/teams/:id(.:format)      admin/teams#show
                PATCH  /admin/users/teams/:id(.:format)      admin/teams#update
                PUT    /admin/users/teams/:id(.:format)      admin/teams#update
                DELETE /admin/users/teams/:id(.:format)      admin/teams#destroy