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"}
我在应用程序目录的机构目录内有一个学生控制器。它具有以下结构。
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
我在这里缺少什么。
提前致谢。
I am having problem with namespaced controller and routing. I have following namespaced route.
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
it generated the following routes
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"}
I have a students controller inside institution directory in apps directory. And it has following structure.
class Institution::StudentsController < ApplicationController
before_filter :require_login
load_and_authorize_resource
..........
.........
end
Now when i tried to redirect to students show page with a link_to helper method as shown below
<%= link_to "show", institution_student_path(student) %>
then it showed wired kind of link with a period(.) in it. Say the student id is 100 and it generated the routes like this
institution/students/4.100
here 4 is current institution id i guess. Why is such routes being generated.
And when i click on show link it gave me error saying
Expected /home/gagan/projects/App_name/app/Institution/students_controller.rb to define StudentsController
What am i missing here.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
or 。
如果学生和机构之间存在关联,
you should be using
or
if there is an association between student and institution.
没关系,我得到了答案。实际上,我将机构文件夹放在控制器文件夹之外,这就是该文件夹内的控制器无法被识别的原因。从而解决了问题。
Never mind I got the answer. Actually I was putting institution folder outside controller folder which was the reason that the controller inside this folder are not being recognized. Hence solved the problem.