rwehresmann
rwehresmann

Reputation: 1168

Define a custom route with an extra id

My Team controller with a custom action named list_questions needs to receive an extra id, the :exercise_id. Here is how am I trying to do this:

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

However, I receive the following error: ArgumentError: Missing :action key on routes definition, please check your routes.

What is the better way to accomplish that?

Upvotes: 0

Views: 201

Answers (2)

jethroo
jethroo

Reputation: 2124

Would just follow your ressourceful routes approach like:

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

will generate:

        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

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

Upvotes: 0

rogelio
rogelio

Reputation: 1571

You are getting the error because the action for that route is not defined.

try something like this

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

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

and you can build the url this way:

list_questions_teams_path(@team, @exercise)

Upvotes: 1

Related Questions