rails路由嵌套资源

时间:2012-01-27 09:31:56

标签: ruby-on-rails ruby-on-rails-3 routes url-routing

我有下一个资源

 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

是否有某种快捷方式可以禁用所有操作,而不是写入所有默认操作:except option ????

2 个答案:

答案 0 :(得分:15)

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

答案 1 :(得分:1)

您可以按照此路线

  resources :topics do
    resources :solutions
  end

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