2017-03-03 82 views
0

我的Team控制器需要自定義動作list_questions需要接收額外的編號:exercise_id。下面是我怎麼想這樣做:定義具有附加標識的自定義路由

resources :teams do 
    member do 
    post :enroll 
    post :unenroll 
    get 'exercises/:exercise_id/list_questions' 
    end 
end 

不過,我收到以下錯誤:ArgumentError: Missing :action key on routes definition, please check your routes.

有什麼更好的方式來實現這一目標?

+0

您必須瞭解'member'和'collection'之間的區別。 「成員」路線將需要一個「ID」,因爲它會影響成員。一個「集合」路線並不是因爲它作用於一個對象集合 –

+0

Rigth。我的解釋是:我仍然需要我的'team_id',所以我把我想要的路線放在我的'團隊'資源的'member'塊中。 – rwehresmann

回答

1

您正在收到錯誤,因爲該路線的操作未定義。

嘗試這樣的事情

resources :teams do 
    member do 
    post :enroll 
    post :unenroll 

    get 'list_questions(/exercises/:exercise_id)', 
     to: "teams#list_questions", 
     as: :list_questions 
    end 
end 

,你可以建立的網址是這樣的:

list_questions_teams_path(@team, @exercise) 
0

只想跟着你ressourceful路線接近,如:

resources :teams do 
    member do 
    post :enroll 
    post :unenroll 
    resources :exercises do 
     member do 
     resources :list_questions 
     end 
    end 
    end 
end 

將產生:

 enroll_team POST  /teams/:id/enroll(.:format)             teams#enroll 
     unenroll_team POST  /teams/:id/unenroll(.:format)             teams#unenroll 
    list_questions GET  /teams/:id/exercises/:id/list_questions(.:format)        list_questions#index 
        POST  /teams/:id/exercises/:id/list_questions(.:format)        list_questions#create 
    new_list_question GET  /teams/:id/exercises/:id/list_questions/new(.:format)       list_questions#new 
edit_list_question GET  /teams/:id/exercises/:id/list_questions/:id/edit(.:format)      list_questions#edit 
     list_question GET  /teams/:id/exercises/:id/list_questions/:id(.:format)       list_questions#show 
        PATCH /teams/:id/exercises/:id/list_questions/:id(.:format)       list_questions#update 
        PUT  /teams/:id/exercises/:id/list_questions/:id(.:format)       list_questions#update 
        DELETE /teams/:id/exercises/:id/list_questions/:id(.:format)       list_questions#destroy 
      exercises GET  /teams/:id/exercises(.:format)             exercises#index 
        POST  /teams/:id/exercises(.:format)             exercises#create 
     new_exercise GET  /teams/:id/exercises/new(.:format)            exercises#new 
     edit_exercise GET  /teams/:id/exercises/:id/edit(.:format)          exercises#edit 
      exercise GET  /teams/:id/exercises/:id(.:format)            exercises#show 
        PATCH /teams/:id/exercises/:id(.:format)            exercises#update 
        PUT  /teams/:id/exercises/:id(.:format)            exercises#update 
        DELETE /teams/:id/exercises/:id(.:format)            exercises#destroy 
       teams GET  /teams(.:format)                teams#index 
        POST  /teams(.:format)                teams#create 
      new_team GET  /teams/new(.:format)               teams#new 
      edit_team GET  /teams/:id/edit(.:format)              teams#edit 
       team GET  /teams/:id(.:format)               teams#show 
        PATCH /teams/:id(.:format)               teams#update 
        PUT  /teams/:id(.:format)               teams#update 
        DELETE /teams/:id(.:format)               teams#destroy 

參見http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default