If I were you, I make this way:
Define routes:
resources :categories do
resources :categories, path: '/'
end
Which rake routes
show:
category_categories GET /categories/:category_id(.:format) categories#index
POST /categories/:category_id(.:format) categories#create
new_category_category GET /categories/:category_id/new(.:format) categories#new
edit_category_category GET /categories/:category_id/:id/edit(.:format) categories#edit
category_category GET /categories/:category_id/:id(.:format) categories#show
PATCH /categories/:category_id/:id(.:format) categories#update
PUT /categories/:category_id/:id(.:format) categories#update
DELETE /categories/:category_id/:id(.:format) categories#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
On the categories_controller
you can identify a nested category (subcategory) because params[:category_id]
is not nil
. And if it is nil
the action is for a parent category.
Edit
Added path
option.
I think you want this:
category_categories GET /:category_id(.:format) categories#index
POST /:category_id(.:format) categories#create
new_category_category GET /:category_id/new(.:format) categories#new
edit_category_category GET /:category_id/:id/edit(.:format) categories#edit
category_category GET /:category_id/:id(.:format) categories#show
PATCH /:category_id/:id(.:format) categories#update
PUT /:category_id/:id(.:format) categories#update
DELETE /:category_id/:id(.:format) categories#destroy
categories GET / categories#index
POST / categories#create
new_category GET /new(.:format) categories#new
edit_category GET /:id/edit(.:format) categories#edit
category GET /:id(.:format) categories#show
PATCH /:id(.:format) categories#update
PUT /:id(.:format) categories#update
DELETE /:id(.:format) categories#destroy
you must do the same on the outer route:
resources :categories, path: '/' do
resources :categories, path: '/'
end