2016-12-01 54 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