2016-12-01 2 views
0

私はモデルのCronJobモデルとhas_and_belongs_to_manyの関係を持っています。Rails 4 HABTM関係を管理するためのネストされたルート

クライアントに、自分自身のcronのリストを有効/無効にすることができます。CronJobsControllerとは別のコントローラーをapp/controllers/cron_jobs_controller.rbに作成します。私はクライアントフォルダの下にきちんと名前空間を付けて、それを単にCronsControllerapp/controllers/clients/crons_controller.rb)と呼んでいます。 >/clients

  • client_crons - - >/clients/:client_id/crons
  • append_client_cron - >post - >/clients/:client_id/crons/:id
    • clientsを:質問は、私は私に利用できるこれらのルートを取得するので、ルートファイルを設定する方法であります
    • remove_client_cron - >delete - >/clients/:client_id/crons/:id

    R

    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/clients/:client_id/clients/crons/です:IGHT今私routes.rbは近いが、その結果ではない、かなり

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

    である、これを持っています。

    名前空間をそのまま残しておいて、目的のルートを得ることができますが、これにより、さまざまなモデルでこれらの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

    関連する問題