有没有办法为仅定义的资源设置控制器操作:或者除外:?

时间:2014-12-06 00:02:14

标签: ruby-on-rails ruby-on-rails-4.1

我有一些嵌套的嵌套资源:

resources :assessments do
    member do
        get 'result'
    end
    resources :respondents, only: [:new, :create] do
      collection do
        post "invite", to: :invite_all
        get "invite", to: :new_invite
      end
    end
  end

对于行resources :respondents, only: [:new, :create],是否可以为新行动和行李箱动作设置动作?您可以使用to:为单个资源设置操作。如果我可以并且保持足够的资源,我想避免写匹配语句。

我要求这样做的动机是,我希望能够为嵌套资源指定操作,而不是让它路由到子资源的操作。例如:

如果我定义

resources :assessments do
    resources :respondents
end

路径/assessments/:id/respondents/new将路由到respondents#new。它的问题迫使我向新操作添加逻辑以确定路由是否包含评估ID,然后呈现正确的视图。我希望能够将嵌套资源发送到其他操作。有没有“铁路”来做到这一点?

1 个答案:

答案 0 :(得分:0)

为什么不直接嵌套这样的资源:

resources :assessments do
  member do
    get 'result'
  end
end

resources :respondents, only: [:new, :create] do
  collection do
    post "invite", to: :invite_all
    get "invite", to: :new_invite
  end
end

或者为其他操作(例如索引)保持嵌套只需单独定义新操作和创建操作。

resources :assessments do
  member do
    get 'result'
  end
  resources :respondents
end

resources :respondents, only: [:new], path_names: { new: 'make' } do
  collection do
    post :generate
    post "invite", to: :invite_all
    get "invite", to: :new_invite
  end
end

这将创建这些路线:

         result_assessment GET    /assessments/:id/result(.:format)                          assessments#result
    assessment_respondents GET    /assessments/:assessment_id/respondents(.:format)          respondents#index
                           POST   /assessments/:assessment_id/respondents(.:format)          respondents#create
 new_assessment_respondent GET    /assessments/:assessment_id/respondents/new(.:format)      respondents#new
edit_assessment_respondent GET    /assessments/:assessment_id/respondents/:id/edit(.:format) respondents#edit
     assessment_respondent GET    /assessments/:assessment_id/respondents/:id(.:format)      respondents#show
                           PATCH  /assessments/:assessment_id/respondents/:id(.:format)      respondents#update
                           PUT    /assessments/:assessment_id/respondents/:id(.:format)      respondents#update
                           DELETE /assessments/:assessment_id/respondents/:id(.:format)      respondents#destroy
               assessments GET    /assessments(.:format)                                     assessments#index
                           POST   /assessments(.:format)                                     assessments#create
            new_assessment GET    /assessments/new(.:format)                                 assessments#new
           edit_assessment GET    /assessments/:id/edit(.:format)                            assessments#edit
                assessment GET    /assessments/:id(.:format)                                 assessments#show
                           PATCH  /assessments/:id(.:format)                                 assessments#update
                           PUT    /assessments/:id(.:format)                                 assessments#update
                           DELETE /assessments/:id(.:format)                                 assessments#destroy
      generate_respondents POST   /respondents/generate(.:format)                            respondents#generate
        invite_respondents POST   /respondents/invite(.:format)                              respondents#invite_all
                           GET    /respondents/invite(.:format)                              respondents#new_invite
            new_respondent GET    /respondents/make(.:format)                                respondents#new

这样你就可以获得新的并在嵌套评估之外创建。

我在本地快速创建了这些路由,这是运行rake routes时生成的。