0

我经常在嵌套资源上使用浅层路由,所以以为我错过了一些简单的东西,但不明白为什么我会收到路由错误。students#show

我有一所学校,有两个嵌套资源:课程和学生。所有操作都适用于课程。:index、:new 和 :create 操作适用于学生。

为什么我会收到路由错误,而不是如中所示/students/3被路由到?students#showrake routes

例外/students/3

Routing Error
No route matches {:controller=>"students"}

Try running rake routes for more information on available routes.

以下是相关位...

耙路线输出:

...
            school_students GET    /schools/:school_id/students(.:format)     students#index
                            POST   /schools/:school_id/students(.:format)     students#create
         new_school_student GET    /schools/:school_id/students/new(.:format) students#new
               edit_student GET    /students/:id/edit(.:format)               students#edit
                    student GET    /students/:id(.:format)                    students#show
                            PUT    /students/:id(.:format)                    students#update
                            DELETE /students/:id(.:format)                    students#destroy
             approve_course GET    /courses/:id/approve(.:format)             courses#approve
             publish_course GET    /courses/:id/publish(.:format)             courses#publish
             school_courses GET    /schools/:school_id/courses(.:format)      courses#index
                            POST   /schools/:school_id/courses(.:format)      courses#create
          new_school_course GET    /schools/:school_id/courses/new(.:format)  courses#new
                edit_course GET    /courses/:id/edit(.:format)                courses#edit
                     course GET    /courses/:id(.:format)                     courses#show
                            PUT    /courses/:id(.:format)                     courses#update
                            DELETE /courses/:id(.:format)                     courses#destroy
                    schools GET    /schools(.:format)                         schools#index
                            POST   /schools(.:format)                         schools#create
                 new_school GET    /schools/new(.:format)                     schools#new
                edit_school GET    /schools/:id/edit(.:format)                schools#edit
                     school GET    /schools/:id(.:format)                     schools#show
                            PUT    /schools/:id(.:format)                     schools#update
                            DELETE /schools/:id(.:format)                     schools#destroy
...

路线.rb

Lms::Application.routes.draw do
...
resources :schools, :shallow => true do
  resources :students
  resources :courses do         # Courses still work if I remove this block.
    member do
      get 'approve'
      get 'publish'
    end
  end
end
...

控制器和视图

我检查了,但由于这是一个路由异常,我认为没有达到此代码。如果你告诉我,我可以添加它们。

图书馆

我将cancanauthlogic一起使用,但从未遇到过路由错误。

4

1 回答 1

0

是的,这很简单。我做了一个错误的假设,即路由错误意味着未到达控制器和视图……而这个“天才”忘记了日志具有完整的堆栈跟踪:

Completed 500 Internal Server Error in 89ms

ActionController::RoutingError (No route matches {:controller=>"students"}):
  app/views/shared/_breadcrumbs.html.erb:27:in `_app_views_shared__breadcrumbs_html_erb___658518109680707361_70357170598800'
  app/views/layouts/application.html.erb:45:in `_app_views_layouts_application_html_erb__2976677320852012228_70357170792580'
  app/controllers/students_controller.rb:19:in `show'

我的面包屑试图构建一个students#index不存在的链接。面包屑尝试处理这种情况(第 25 行),但检查嵌套资源失败:

25: <% elsif controller.class.action_methods.include?('index') %>
26:   <li>
27:     <%= link_to controller_name.titleize, :controller => controller_name, :action => 'index' %>
28:     <span class="divider">/</span>
29:   </li>
于 2012-12-04T14:47:52.957 回答