Rails: how to format and organize routes

I'm new to rails (building my first app) and right now my .rb routes are pretty messy. I was wondering what is the best way to organize / format all the content, so it’s easy to see what happens and avoid stupid 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] 
+5
source share
1 answer

Unfortunately, this is just part of the rails that this file becomes erratic over time. Our application contains hundreds of entries for various items that have been added over the years, so I know from my own experience what you can think of from the very beginning.

The number one function you can do to save an organized file is to add a large number of comments with some consistency, which will help you understand how they fit your application, for example:

 # ADMIN FUNCTIONALITY # -- Allows super admin access and functionality # your admin stuff here 

And then save your routes for certain functions in the same section. In your example, you have a β€œteacher” route near the top, and then a little lower. Keep them grouped and commented on and they will ultimately be easier to manage.

+4
source

Source: https://habr.com/ru/post/1236641/


All Articles