2012-01-27 234 views
7

我旁邊資源軌路由嵌套資源

resources :countries do 
    resources :cities 
end  

resources :cities do 
    resources :streets 
end 

它生成下一路線

GET /countries/:country_id/cities(.:format)       cities#index 
POST /countries/:country_id/cities(.:format)       cities#create 
new_country_city GET /countries/:country_id/cities/new(.:format)      cities#new 
edit_country_city GET /countries/:country_id/cities/:id/edit(.:format)     cities#edit 
GET /countries/:country_id/cities/:id(.:format)      cities#show 
PUT /countries/:country_id/cities/:id(.:format)      cities#update 
DELETE /countries/:country_id/cities/:id(.:format)      cities#destroy 


...... 
cities GET /cities(.:format)            cities#index 
POST /cities(.:format)            cities#create 
new_city GET /cities/new(.:format)           cities#new 
edit_city GET /cities/:id/edit(.:format)          cities#edit 
city GET /cities/:id(.:format)           cities#show 
PUT /cities/:id(.:format)           cities#update 
DELETE /cities/:id(.:format)           cities#destroy 

我不想進入城市可以沒有國家ID也是我不想用3水平嵌套的資源,所以我可以改變路線如下

resources :countries do 
     resources :cities 
    end  

    resources :cities, :except => [:index, :destroy, :edit, :show, :create, :new, :update] do 
     resources :streets 
    end 

是否有某種快捷方式禁用所有操作寫下所有默認操作:除選項????

回答

15
resources :cities, :only => [] do 
    ... 
end 
+0

啊容易....感謝 – Fivell 2012-01-27 09:54:53

+0

在Rails 2.2,你可以做':除了=>:all'但也不不再起作用。謝謝你。 – Ashitaka 2013-01-18 00:54:49

1

你可以按照這個路線

 
    resources :topics do 
    resources :solutions 
    end 

    resources :solutions, only: [] do 
    resources :reviews, except: [:show, :index] 
    end