shankardevy
shankardevy

Reputation: 4370

Change the scope of routes to 'admin' for specific actions

I have a category model and in my routes.rb, I have

resources :categories

which generates the following set of routes.

categories_path      GET     /categories(.:format)           categories#index
                     POST    /categories(.:format)           categories#create
new_category_path    GET     /categories/new(.:format)       categories#new
edit_category_path   GET     /categories/:id/edit(.:format)  categories#edit
category_path        GET     /categories/:id(.:format)       categories#show
                     PATCH   /categories/:id(.:format)       categories#update
                     PUT     /categories/:id(.:format)       categories#update
                     DELETE  /categories/:id(.:format)       categories#destroy

Now, what I need is except for all GET routes, I want the rest of the routes to be under '/admin' scope. So that operations like create, edit and delete are accessed at admin/categories/:id/edit etc.

Is there an easy way to mention this scope?

Upvotes: 0

Views: 502

Answers (2)

Mostafa Hussein
Mostafa Hussein

Reputation: 11940

You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an admin namespace. You would place these controllers under the app/controllers/admin directory, and you can group them together in your router:

namespace "admin" do
  resources :posts, :comments
end

This will create a number of routes for each of the posts and comments controller. For Admin::PostsController, Rails will create:

GET       /admin/posts
GET       /admin/posts/new
POST      /admin/posts
GET       /admin/posts/1
GET       /admin/posts/1/edit
PATCH/PUT /admin/posts/1
DELETE    /admin/posts/1

check the rest of it through the apidock documentation

Upvotes: 1

Bigxiang
Bigxiang

Reputation: 6692

I think you can define route of categories twice.

resources :categories, :only => :index
resources :categories, :except => :index, :path => 'admin/categories'

Upvotes: 0

Related Questions