如何解决Rails中嵌套路线的此错误?

时间:2018-10-15 03:46:09

标签: ruby-on-rails ruby-on-rails-5

我有一个在Rails中实现的应用程序,发生的事情是我想访问以下URL:http://localhost:3000/academics/students/1/schedules,但是在尝试访问以下错误时出现:Routing Error uninitialized constant Academics::SchedulesController

问题是我无法为嵌套资源执行此操作,因为已经存在另一条路线的学生的日程安排,而先前的学术路线是针对管理员和其他独立路线的。通过以下方式创建了学者中的进度控制器:class Academics::Students::SchedulesController < ApplicationController

有什么办法可以与以前的实现一起使用?

route.rb资源学者:

  namespace :academics do
    resources :subjects
    namespace :students do
      resources :schedules
    end
    resources :groups
  end

谢谢。

1 个答案:

答案 0 :(得分:0)

如果您这样做:

namespace :academics do
  resources :subjects
  resources :students do
    scope module: :students do 
      resources :schedules
    end
  end
  resources :groups
end

然后您将获得(除其他外):

    academics_student_schedules GET    /academics/students/:student_id/schedules(.:format)          academics/students/schedules#index
                                POST   /academics/students/:student_id/schedules(.:format)          academics/students/schedules#create
 new_academics_student_schedule GET    /academics/students/:student_id/schedules/new(.:format)      academics/students/schedules#new
edit_academics_student_schedule GET    /academics/students/:student_id/schedules/:id/edit(.:format) academics/students/schedules#edit
     academics_student_schedule GET    /academics/students/:student_id/schedules/:id(.:format)      academics/students/schedules#show
                                PATCH  /academics/students/:student_id/schedules/:id(.:format)      academics/students/schedules#update
                                PUT    /academics/students/:student_id/schedules/:id(.:format)      academics/students/schedules#update
                                DELETE /academics/students/:student_id/schedules/:id(.:format)      academics/students/schedules#destroy

这将路由:

/academics/students/1/schedules 

执行index的{​​{1}}操作。

顺便说一句,我不确定您的问题定义中的类定义(即Academics::Students::SchedulesController)中是否确实有空格。如果是这样,那对我来说似乎很不寻常。但是,如果可行,那将很有趣。