Rails 3 命名空间控制器和路由

3

我在使用命名空间控制器和路由时遇到了问题。我有以下的命名空间路由。

  namespace :institution do
    resources :students
  end

along with

  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"}

我有一个学生控制器,位于应用程序目录中的机构目录内。它的结构如下:

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

我在这里漏掉了什么。

提前感谢。

2个回答

1

你应该使用

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

或者

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

如果学生和机构之间存在关联。

我认为你没有完全理解我的问题,请看路由生成中的第5行。尽管我使用了institution_student_path(:student_id)来生成链接,但它生成的路径与上述描述不符。为什么会这样?当我尝试访问institution_students_path时,我会得到一个错误,如问题的最后一行所示。 - Gagan

1
没事了,我找到答案了。实际上,我把机构文件夹放在控制器文件夹外面,这就是导致该文件夹内的控制器无法被识别的原因。因此问题已解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接