0

我有这个

devise_for :users
 resources :sites do
  resources :users
  get 'companies/search'
 end
 root :to => "welcome#index"
end

路线看起来像这样

       site_users GET    /sites/:site_id/users(.:format)            users#index
                  POST   /sites/:site_id/users(.:format)            users#create
    new_site_user GET    /sites/:site_id/users/new(.:format)        users#new
   edit_site_user GET    /sites/:site_id/users/:id/edit(.:format)   users#edit
        site_user GET    /sites/:site_id/users/:id(.:format)        users#show
                  PUT    /sites/:site_id/users/:id(.:format)        users#update
                  DELETE /sites/:site_id/users/:id(.:format)        users#destroy
 site_companies_search GET /sites/:site_id/companies/search(.:format)  sites/:site_id/companies#search
            sites GET    /sites(.:format)                           sites#index
                  POST   /sites(.:format)                           sites#create
         new_site GET    /sites/new(.:format)                       sites#new
        edit_site GET    /sites/:id/edit(.:format)                  sites#edit
             site GET    /sites/:id(.:format)                       sites#show
                  PUT    /sites/:id(.:format)                       sites#update
                  DELETE /sites/:id(.:format)                       sites#destroy
             root        /                                          welcome#index

但我想知道是否有办法拥有这样的路线

       site_users GET    /:site_id/users(.:format)            users#index
                  POST   /:site_id/users(.:format)            users#create
    new_site_user GET    /:site_id/users/new(.:format)        users#new
   edit_site_user GET    /:site_id/users/:id/edit(.:format)   users#edit
        site_user GET    /:site_id/users/:id(.:format)        users#show
                  PUT    /:site_id/users/:id(.:format)        users#update
                  DELETE /:site_id/users/:id(.:format)        users#destroy

没有 /sites 在 url 所以 url 来自

/sites/some_name/users 
/sites/some_name/users/new

/some_name/users 
/some_name/users/new

如果可能的话,这将清理 url

4

2 回答 2

2
resources :sites do
  get 'companies/search'
end
scope ':site_id' do
  resources :users
end
root :to => "welcome#index"
于 2012-05-22T20:34:54.200 回答
1

如果您希望能够使用它以及像继承资源这样的资源加载机制并且在siteCRUD 操作/,您可以绘制您的路线,例如:

resources :sites, :path => "" do
  get 'companies/search'

  resources :users
end

root :to => "welcome#index"
于 2012-05-22T21:00:29.337 回答