1

I am wondering if the following is acceptable structure for the routes in the route file. I have not seen an example of it being done exactly this way, however it does seem logical. If it is not please let me know why.

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/ 
The Gugaru
  • 618
  • 6
  • 23
  • 5
    You can run `rake routes` to check the routes yourself. – Arie Xiao Apr 23 '15 at 05:24
  • 1
    @ArieShaw I am asking if it is an accepted structuring of the routes by nesting them in the manner I have. Rake works without issue! I just wanted to know if this goes against any ROR Programming Paradigms. – The Gugaru Apr 23 '15 at 06:28
  • 1
    structure looks fine to me, nested structure is allowed using `namespace` in Rails, but one should not use deep level of nesting unless there is absolute need – Pramod Shinde Apr 23 '15 at 07:03
  • The way you comment the routes file is very weird to me :) – Wazery Apr 23 '15 at 07:14

1 Answers1

2

@ArieShaw is right. If rake routes works it is 'acceptable'.

However, creating your routes like this will mean you will be missing out on some of the magic rails gives you. In particular, you'll need to create custom controller actions.

For example, your sales area:

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

I'd refactor that and create Lead and ContactLog objects (perhaps within a Sales namespace module). Then I'd use:

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

That will give you:

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

And those actions will all match index, new, create, edit, update, show, and destroy methods on the LeadsController and ContactLogsController controllers.

I'd also not handle login within the sales namespace, but rather using a gem such as Devise.

For more information, have a look at this: http://guides.rubyonrails.org/routing.html

In summary: What you are doing in your example is not wrong, and if you are being asked to create those specific paths, it may well be the a fine way of doing it. However, you are making your work a little harder than it may need to be, by not using some of the rails restful routes.

ReggieB
  • 8,100
  • 3
  • 38
  • 46
  • 1
    Thank you! That is what I was concerned about. So in that case I will refactor back to what I had and then do resources. Your response was most helpful giving it a +1 – The Gugaru Apr 23 '15 at 08:26