2015-11-23 27 views
5

我是一個rails新手(構建我的第一個應用程序),現在我的routes.rb是相當混亂。我想知道組織/格式化所有內容的最佳方式是什麼,以便很容易看出發生了什麼並避免愚蠢的路由錯誤。Rails:如何格式化和組織路線

任何一般的提示或簡化的例子,將不勝感激。

的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] 
+0

除了您重複歡迎索引路線兩次之外,它對我來說看起來不錯。如果你想讓它成爲你的根路由,只需使用'root'welcome#index''並刪除它上面的get請求。 – Cyzanfar

+0

我會建議問這個問題是StackOverflow聊天,如果你有足夠的代表。此外,我建議刪除過多的間距。我在代碼段之間使用不超過1個空格。 – onebree

回答

4

可惜這只是鋼軌的一部分,這個文件隨着時間的推移變得混亂。我們的應用程序擁有數百項多年來添加的各種項目,所以我從經驗中知道從一開始就很好思考。

一兩件事可以做,以保持組織的文件中添加大量的註釋,以某種一致性,幫助您瞭解它們如何應用相符,例如:

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

然後在同一部分中保留路由的某些功能。在你的例子中,你有一個靠近頂部的「教師」路線,然後靠近底部。將這些分組在一起並進行評論,從長遠來看,管理起來會更容易。