Ruby On Rails和Routes文件结构

时间:2015-04-23 05:09:04

标签: ruby-on-rails ruby nested-routes

我想知道以下路由文件中的路由是否可接受。我还没有看到它正是以这种方式完成的一个例子,但它似乎合乎逻辑。如果不是,请告诉我原因。

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/ 

1 个答案:

答案 0 :(得分:2)

@ArieShaw是对的。如果佣金路线有效,那么它是可以接受的。

但是,创建这样的路线意味着你会错过一些魔术轨道给你的。特别是,您需要创建自定义控制器操作。

例如,您的销售区域:

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控制器上的索引,新建,创建,编辑,更新,显示和销毁方法。

我也没有处理销售命名空间内的登录,而是使用Devise等宝石。

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

总结:您在示例中所做的事情没有错,如果要求您创建这些特定路径,那么这可能是一种很好的方法。但是,通过不使用一些固定的路线,你正在使你的工作变得比它可能需要的更难。