I am a rails newbie (building my first app) and right now my routes.rb is quite a mess. I was wondering what is the best way to organize/format all the content so it is easy to see what is going on and avoid silly routing errors.
Any general tips or simplified examples would be appreciated.
Routes.rb
Rails.application.routes.draw do
resources :posts
get 'users/index'
#devise_for :admins
namespace :super_admin do #superadmin stuff
resources :dashboard, only: [:index]
end
devise_for :super_admins, path: "super_admin", controllers: { registrations: "registrations", sessions: "super_admin/sessions" } #lets super admin sign in
get 'welcome/index'
root to: "welcome#index"
match '/teachers', to: 'teachers#index', via: 'get'
#route to delete users
match 'users/:id' => 'users#destroy', :via => :delete, :as => :admin_destroy_user
match '/users/:id', to: 'users#show', via: 'get'
#routes for registration
devise_for :users, controllers: { registrations: "registrations" }
devise_for :teachers, controllers: { registrations: "teacher/registrations" }
get 'users/:id/posts' => 'users#posts', :as => :user_posts
match '/users', to: 'users#index', via: 'get'
match '/about', to: 'about#index', via: 'get'
match '/teachers/:id', to: 'teachers#show', via: 'get'
match '/teachers/list', to: 'teachers#list', via: 'get'
get 'super_admin/dashboard/new_user', :as => :super_admin_new_user
resources :users, :only =>[:show]
Unfortunately it's simply part of rails that this file gets messy over time. Our app has hundreds of entries for various items that have been added over the years, so I know from experience it's good to think of from the beginning.
The number one thing you can do to keep the file organized is to add lots of comments, with some kind of consistency that helps you understand how they match your app, for example:
# ADMIN FUNCTIONALITY
# -- Allows super admin access and functionality
# your admin stuff here
And then keep your routes for certain functionality in the same section. In your example, you have a "teachers" route near the top, and then some more near the bottom. Keep those grouped together and commented and it'll be easier to manage in the long run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With