2012-02-15 122 views
3

我遇到命名空間控制器和路由問題。我有以下命名空間的路線。rails 3命名空間控制器和路由

namespace :institution do 
    resources :students 
    end 

resources :students, :only => [] do 
    resources :college_selections, :only =>[:index, :create, :update] 
    resources :progress, :only => [:index] do 
     collection {get 'compare'} 
    end 
    resources :marks 
    end 

沿着它產生以下路線

 institution_students GET /institution/students(.:format)     {:action=>"index", :controller=>"institution/students"} 
          POST /institution/students(.:format)     {:action=>"create", :controller=>"institution/students"} 
    new_institution_student GET /institution/students/new(.:format)    {:action=>"new", :controller=>"institution/students"} 
    edit_institution_student GET /institution/students/:id/edit(.:format)  {:action=>"edit", :controller=>"institution/students"} 
     institution_student GET /institution/students/:id(.:format)    {:action=>"show", :controller=>"institution/students"} 
          PUT /institution/students/:id(.:format)    {:action=>"update", :controller=>"institution/students"} 
          DELETE /institution/students/:id(.:format)    {:action=>"destroy", :controller=>"institution/students"} 

我在apps目錄機構目錄內的學生控制器。它具有以下結構。

class Institution::StudentsController < ApplicationController 
    before_filter :require_login 
    load_and_authorize_resource 
.......... 
......... 
end 

現在,當我試圖重定向到學生展示用的link_to輔助方法,頁面如下圖所示

<%= link_to "show", institution_student_path(student) %> 

則表明有線怎樣的聯繫與句號(。)在裏面。說學生ID是100,它產生了這樣的路線

institution/students/4.100 

這裏4是我猜的當前機構ID。爲什麼會產生這樣的路線。

當我上節目鏈接點擊它給了我錯誤說

Expected /home/gagan/projects/App_name/app/Institution/students_controller.rb to define StudentsController 

我錯過了什麼。

在此先感謝。

回答

1

沒關係我得到了答案。其實我正在控制器文件夾之外的機構文件夾,這是該文件夾內的控制器不被識別的原因。因此解決了這個問題。

1

你應該使用

<%= link_to "show", institution_student_path(institution, student) %> 

<%= link_to "show", institution_student_path(student.institution, student) %> 

,如果有學生和機構之間的關聯。

+0

我想你並沒有完全理解我的問題,請參閱生成的路由中的第5行。即使我生成了與institution_student_path(:student_id)的鏈接,它會生成上面描述的錯誤路徑。這是爲什麼?當我嘗試去institution_students_path我得到一個錯誤,顯示在問題的最後一行。 – Gagan 2012-02-18 16:13:51