Get unused routes of large Rails App šŸ› 

Rishi Pithadiya
Techieā€™s Toolkit
2 min readOct 2, 2019

--

After a few years of continuous development, your rails application becomes larger and itā€™s good practice to do some cleanup. One of the most obvious cleanups is cleaning up unused routes.

As your rails application grows, at the time of adding a route for new action, developers sometimes add resources :objects in config/routes.rb file rather than adding a single member or collection route which creates unnecessary routes at runtime šŸ˜–

For example,

config/routes.rb

Writing above code will create the following routes -

Routes for user's resources and their associated actions.

But, it might be possible that we are not using all of the above methods, or we need only users#new, users#create and users#show methods in our Users controller like -

users_controlle.rb

So, itā€™s the most obvious process to inspect your routes.rb file from time to time and clean it if necessary.

Inspecting each route and its corresponding action took so much time and itā€™s next to impossible thing when the application is so large šŸ˜ž

Now, there are few ruby gems available in the market which can do it for you, but again adding additional gem requires extra load in your gemfile of your large application.

Another solution is to make a simple script which compares routes and associated actions available in controllers and list down all possible unused routes of our large application so that we can clean routes.rb file and remove routes that are not needed.

Have a look at following a small ruby script -

Script to get unused routes for your large Rails application

For getting unused routes for your rails app, all you need to do is to copy above code in your rails console or you can paste above code in single file get-unused-routes.rb in the root directory of your Rails application and run it using the following command -

$> ruby get-unused-routes.rb 

Using above small script weā€™re able to get all unused routes of our large rails application, we donā€™t use any Gem/Plugin to achieve this šŸ„³

References -

Found this post useful? please click the šŸ‘ button and share to help others find it! Feel free to leave a comment below.

--

--

Rishi Pithadiya
Techieā€™s Toolkit

Finding happiness in words, food, places & untold stories āœŒšŸ¼ rishi.tips