Rails路由中的嵌套资源冲突

时间:2012-06-25 00:14:27

标签: ruby-on-rails ruby-on-rails-3

通过以下路线,我遇到了冲突问题。似乎两者不能一起存在。如何在没有冲突的情况下指定两者?感谢。

 resources :colleges do
    resources :departments
 end

 resources :departments do
    resources :professors
 end   

路径 new_college_department_path(@college)会生成网址学校/ 1 / departments / new ,从而导致以下错误。我不确定为什么会列出控制器教授,因为它甚至不是路线的一部分!

 Routing Error

No route matches {:controller=>"professors", :format=>nil, :request_id=>#<department_id: nil, text: nil, goal: nil, accept: nil, post_url: nil, created_at: nil, updated_at: nil, u_id: nil, b_id: 1, active: nil>}

路线:

blog_requests GET    /blogs/:blog_id/requests(.:format)                  requests#index
                       POST   /blogs/:blog_id/requests(.:format)                  requests#create
      new_blog_request GET    /blogs/:blog_id/requests/new(.:format)              requests#new
     edit_blog_request GET    /blogs/:blog_id/requests/:id/edit(.:format)         requests#edit
          blog_request GET    /blogs/:blog_id/requests/:id(.:format)              requests#show
                       PUT    /blogs/:blog_id/requests/:id(.:format)              requests#update
                       DELETE /blogs/:blog_id/requests/:id(.:format)              requests#destroy
                 blogs GET    /blogs(.:format)                                    blogs#index
                       POST   /blogs(.:format)                                    blogs#create
              new_blog GET    /blogs/new(.:format)                                blogs#new
             edit_blog GET    /blogs/:id/edit(.:format)                           blogs#edit
                  blog GET    /blogs/:id(.:format)                                blogs#show
                       PUT    /blogs/:id(.:format)                                blogs#update
                       DELETE /blogs/:id(.:format)                                blogs#destroy
    request_supporters GET    /requests/:request_id/supporters(.:format)          supporters#index
                       POST   /requests/:request_id/supporters(.:format)          supporters#create
 new_request_supporter GET    /requests/:request_id/supporters/new(.:format)      supporters#new
edit_request_supporter GET    /requests/:request_id/supporters/:id/edit(.:format) supporters#edit
     request_supporter GET    /requests/:request_id/supporters/:id(.:format)      supporters#show
                       PUT    /requests/:request_id/supporters/:id(.:format)      supporters#update
                       DELETE /requests/:request_id/supporters/:id(.:format)      supporters#destroy
              requests GET    /requests(.:format)                                 requests#index
                       POST   /requests(.:format)                                 requests#create
           new_request GET    /requests/new(.:format)                             requests#new
          edit_request GET    /requests/:id/edit(.:format)                        requests#edit
               request GET    /requests/:id(.:format)                             requests#show
                       PUT    /requests/:id(.:format)                             requests#update
                       DELETE /requests/:id(.:format)                             requests#destroy
                       GET    /requests(.:format)                                 requests#index
                       POST   /requests(.:format)                                 requests#create
                       GET    /requests/new(.:format)                             requests#new
                       GET    /requests/:id/edit(.:format)                        requests#edit
                       GET    /requests/:id(.:format)                             requests#show
                       PUT    /requests/:id(.:format)                             requests#update
                       DELETE /requests/:id(.:format)                             requests#destroy
            supporters GET    /supporters(.:format)                               supporters#index
                       POST   /supporters(.:format)                               supporters#create
         new_supporter GET    /supporters/new(.:format)                           supporters#new
        edit_supporter GET    /supporters/:id/edit(.:format)                      supporters#edit
             supporter GET    /supporters/:id(.:format)                           supporters#show
                       PUT    /supporters/:id(.:format)                           supporters#update
                       DELETE /supporters/:id(.:format)                           supporters#destroy
                 users GET    /users(.:format)                                    users#index
                       POST   /users(.:format)                                    users#create
              new_user GET    /users/new(.:format)                                users#new
             edit_user GET    /users/:id/edit(.:format)                           users#edit
                  user GET    /users/:id(.:format)                                users#show
                       PUT    /users/:id(.:format)                                users#update
                       DELETE /users/:id(.:format)                              

2 个答案:

答案 0 :(得分:1)

professorsdepartmentsdepartments的孩子,如果colleges的孩子可以定义如下:

 resources :colleges do
    resources :departments do
      resources :professors
    end
 end

所以,你的道路将是:

colleges/1/departments/1/professors/1

答案 1 :(得分:1)

错误在form_for中。路线没问题。