Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails routing nested resources

I have next resources

 resources :countries do
    resources :cities
 end    

 resources :cities do
    resources :streets
 end

it generates next routes

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

I dont want access to cities can be without country id but also I don't want to use 3-levels nested resources, so I can change routes like next

 resources :countries do
        resources :cities
     end    

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

Is there some kind of shortcut to disable all action instead of write all default actions at :except option ????

like image 882
Fivell Avatar asked Jan 27 '12 09:01

Fivell


2 Answers

resources :cities, :only => [] do
    ...
end
like image 92
PeterWong Avatar answered Oct 19 '22 23:10

PeterWong


You can follow this routes

  resources :topics do
    resources :solutions
  end

  resources :solutions, only: [] do
    resources :reviews, except: [:show, :index]
  end
like image 1
Rokibul Hasan Avatar answered Oct 19 '22 22:10

Rokibul Hasan