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

发布于 2025-01-06 11:07:06 字数 2135 浏览 1 评论 0原文

我在命名空间控制器和路由方面遇到问题。我有以下命名空间路线。

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛溺水了 2025-01-13 11:07:06

您应该使用

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

or 。

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

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

you should be using

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

or

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

if there is an association between student and institution.

冷情 2025-01-13 11:07:06

没关系,我得到了答案。实际上,我将机构文件夹放在控制器文件夹之外,这就是该文件夹内的控制器无法被识别的原因。从而解决了问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文