gpt4 book ai didi

ruby-on-rails - rails : how to format and organize routes

转载 作者:数据小太阳 更新时间:2023-10-29 07:16:45 26 4
gpt4 key购买 nike

我是一个 Rails 新手(构建我的第一个应用程序),现在我的 routes.rb 非常困惑。我想知道组织/格式化所有内容的最佳方式是什么,以便很容易看到发生了什么并避免愚蠢的路由错误。

任何一般提示或简化示例将不胜感激。

路线.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]

最佳答案

不幸的是,随着时间的推移,这个文件变得困惑,这只是 rails 的一部分。我们的应用程序有数百个条目,用于多年来添加的各种项目,所以我从经验中知道,从一开始就想到是件好事。

要使文件井井有条,您可以做的第一件事是添加大量评论,并保持某种一致性,帮助您了解它们与您的应用的匹配程度,例如:

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

然后将某些功能的路由保留在同一部分中。在您的示例中,您在顶部附近有一条“教师”路线,然后在底部附近有更多路线。将这些分组并进行评论,从长远来看,这将更易于管理。

关于ruby-on-rails - rails : how to format and organize routes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33878399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com