Ruby 关于 Rails 和路由文件结构

Ruby On Rails and Routes file structure

我想知道以下是否是路由文件中路由的可接受结构。我还没有看到完全以这种方式完成的示例,但它看起来确实合乎逻辑。如果不是,请告诉我原因。

Rails.application.routes.draw do
### BEGIN /some_base_route/   
namespace :some_base_route do
### BEGIN /some_base_route/lead_vendor 
namespace :lead_vendor do
  get 'import'
  get 'results'
end
### END /some_base_route/lead_vendor 

### BEGIN /some_base_route/sales/   
namespace :sales do
  get 'view_lead'
  get 'edit_lead'
  post 'edit_lead'
  get 'create_contact_log'
  get 'login'
  get 'dashboard'
end
### END /some_base_route/sales/ 

### BEGIN /some_base_route/admin/ 
namespace :admin do
  get 'admin/login'
  get 'admin/dashboard'
end
### END /some_base_route/admin/ 

### BEGIN /some_base_route/process/
namespace :process do
  ### BEGIN /some_base_route/process/sales   
  namespace :sales do
    ### BEGIN /some_base_route/process/sales/leads
    namespace :leads do   
      get 'create'
      get 'edit'
      get 'delete'
      get 'call_log_create'
    end 
    ### END /some_base_route/process/sales/leads
  end
  ### END /some_base_route/process/sales

  ### BEGIN /some_base_route/process/new_client
  namespace :new_client do         
    get 'new_client/step1'
    get 'new_client/step2'
    get 'new_client/step3'
    get 'new_client/step4'
    get 'new_client/step5'
    get 'new_client/step6'
  end
  ### END /some_base_route/process/new_client

  ### BEGIN /some_base_route/process/admin/
  namespace :admin do
    ### BEGIN /some_base_route/process/admin/user_management/
    namespace :user_management do
      get 'create'
      get 'edit'
      get 'delete'
      get 'disable'
    end
    ### END /some_base_route/process/admin/user_management/

    ### BEGIN /some_base_route/process/admin/services/
    namespace :services do
      get 'create'
      get 'edit'
      get 'delete'
    end
    ### END /some_base_route/process/admin/services/
  end
  ### END /some_base_route/process/admin/
end
### END /some_base_route/process/
end
### END /some_base_route/ 

@ArieShaw 是对的。如果 rake 路由有效,它是 'acceptable'.

但是,像这样创建路线意味着您将错过 rails 给您带来的一些魔力。特别是,您需要创建自定义控制器操作。

例如您的销售区域:

namespace :sales do
  get 'view_lead'
  get 'edit_lead'
  post 'edit_lead'
  get 'create_contact_log'
  get 'login'
  get 'dashboard'
end

我会重构它并创建 Lead 和 ContactLog 对象(可能在 Sales 命名空间模块中)。然后我会使用:

namespace :sales do
  resources :leads
  resources :contact_logs, only: [:create, :new] 
end

那会给你:

get sales/leads -  index view of leads (probably equivalent to your dashboard view)
get sales/leads/new - form for new lead
post sales/leads - create a new lead
get sales/leads/:id  - view one lead
get sales/leads/:id/edit - edit a lead
patch sales/leads/:id - update a lead
delete sales/leads/:id - delete a lead
get sales/contact_logs/new - form for new contract log
post sales/contact_logs - create a contract log

这些操作都将匹配 LeadsController 和 ContactLogsController 控制器上的索引、新建、创建、编辑、更新、显示和销毁方法。

我也不会在 sales 命名空间内处理登录,而是使用 gem 例如 Devise.

有关更多信息,请查看此内容:http://guides.rubyonrails.org/routing.html

总而言之:您在示例中所做的并没有错,如果您被要求创建那些特定的路径,这很可能是一种很好的方法。但是,通过不使用某些 rails restful 路线,您的工作比可能需要的要难一些。