多态路由指向相同的控制器和动作

时间:2014-12-11 20:25:56

标签: ruby-on-rails routing polymorphic-associations

这是一种多态关联!

 resources :professors, :labs do
   member do
     resources :teaching_assistants
   end
 end

我希望为教授助教,实验室助教和教学助理提供索引,如下:

/professors/id/teaching_assistants
/labs/id/teaching_assistants
/teaching_assistants

所有这些路线都指向       teaching_assistants#指数。我是否必须明确地将嵌套路由指向教授和实验室控制器中的操作?我该怎么做/改变路线文件?

 resources :professors, :labs do
   member do
     resources :teaching_assistants, except: :index
   end
 end

get '/professors/id/teaching_assistants' => professors#assistantsindex
get '/labs/id/teaching_assistants' => labs#assistantsindex

1 个答案:

答案 0 :(得分:1)

使用 resources 用于创建所有基本RESTful指针(index / create / read / update / delete)。听起来您的助教是一个“一次性”列表页面,在这种情况下,您只需使用 get 来创建路线。

试试这个:

resources :professors, :labs do
    member do
         # /professors/id/teaching_assistants > professors#teaching_assistants
         # /labs/id/teaching_assistants > labs#teaching_assistants
         get 'teaching_assistants'  
    end
end

# /teaching_assistants
get 'teaching_assistants'