2016-12-01 55 views
0

我有一個Client模型,它與CronJob模型有has_and_belongs_to_many關係。Rails 4嵌套路線管理HABTM關係

現在給客戶啓用/取消給定自己的cron列表的能力,我想製作一個獨立於CronJobsControllerapp/controllers/cron_jobs_controller.rb的控制器。我希望它在客戶機文件夾下整潔地放置命名空間,並簡單地稱其爲CronsControllerapp/controllers/clients/crons_controller.rb)。現在的問題是如何設置的路徑文件,這樣我可以得到這些路由我:

  • clients - >/clients
  • client_crons - >/clients/:client_id/crons
  • append_client_cron - >post - >/clients/:client_id/crons/:id
  • remove_client_cron - >delete - >/clients/:client_id/crons/:id

ř現在我的飛行有routes.rb這一點,這是接近,但不是很

resources :clients do 
    namespace :clients do 
     resources :crons, only: ['index'] do 
      member do 
      post :append 
      delete :remove 
      end 
     end 
    end 
    end 

導致:

append_client_clients_cron POST /clients/:client_id/clients/crons/:id/append(.:format)  clients/crons#append 
    remove_client_clients_cron DELETE /clients/:client_id/clients/crons/:id/remove(.:format)  clients/crons#remove 
      client_clients_crons GET /clients/:client_id/clients/crons(.:format)    clients/crons#index 

這裏的問題是/clients/:client_id/clients/crons/在中間這個額外clients

我知道我可以將命名空間從它中取出並獲得所需的路徑,但這會使文件夾體系結構相當笨拙,因爲在各種模型上將存在許多這些HABTM關係。

或者,有沒有一種方法可以告訴路徑文件在crons資源的客戶端子文件夾中查找?

回答

1
resources :clients do 
    scope module: :clients do 
     resources :crons, only: ['index'] do 
     member do 
      post :append 
      delete :remove 
     end 
     end 
    end 
    end 
+0

謝謝。 IDK誰降低了但這對我很好。另外,我將'member'塊更改爲'post'/',以:'crons#append''現在我發佈到'/ clients /:client_id/crons /:id' – Killerpixler