Nilay Singh
Nilay Singh

Reputation: 2341

Rails disable edit update delete routes

I am trying to find a way to disable resource routes like edit destroy and update. It can be done using this answer. Disable Route In this answer I can put code like this:

resources :books, except: [:edit, :destroy]

And it will work but I have a unique problem I have created many resource routes and my route file is like this:

         resources :expenditure_management2s do 
                            collection { post :import }
                            collection { get :dropdown }
                            collection { get :test }
                            end 
        resources :expenditure_management1s do 
                            collection { post :import }
                            collection { get :dropdown }
                            collection { get :test }
                            end 
        resources :expenditure_managements do 
                            collection { post :import }
                            collection { get :dropdown }
                            collection { get :test }
                            end 
                 ......

I have almost 100 routes like this If I have to change these methods one by one it would be a tough task. Is there any way where I can group these route into some method and reject edit update and destroy for all resource route.

Upvotes: 2

Views: 474

Answers (1)

Ovidiu Toma
Ovidiu Toma

Reputation: 91

I think you can you use a scope in your routes.rb file like this:

scope except: [:edit, :destroy] do
  resources :users
end

Will return the routes:

users     GET   /users(.:format)   users#index
          POST  /users(.:format)   users#create
new_user  GET   /users/new(.:format)   users#new
user      GET   /users/:id(.:format)   users#show
          PATCH /users/:id(.:format)   users#update
          PUT   /users/:id(.:format)   users#update

As you can see the users#destroy and users#edit routes are missing.

In your case, it would be:

 scope except: [:edit, :destroy] do
    resources :expenditure_management2s do 
                        collection { post :import }
                        collection { get :dropdown }
                        collection { get :test }
                        end 
    resources :expenditure_management1s do 
                        collection { post :import }
                        collection { get :dropdown }
                        collection { get :test }
                        end 
    resources :expenditure_managements do 
                        collection { post :import }
                        collection { get :dropdown }
                        collection { get :test }
                        end 
 end

Upvotes: 5

Related Questions