使用额外ID定义自定义路由

时间:2017-03-03 13:08:29

标签: ruby-on-rails routes

我的Team控制器具有名为list_questions的自定义操作,需要获得额外的ID :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.

实现这一目标的更好方法是什么?

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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