嵌套浅层路由的Rails路由异常:1个工作,1个不工作

时间:2012-12-04 14:26:40

标签: ruby-on-rails ruby-on-rails-3 rails-routing

我经常在shallow routes上使用nested resources,所以我想错过了一些简单的内容,但不明白我为什么会收到students#show的路由错误。

我的学校有两个嵌套资源:课程和学生。所有行动都适用于课程。 :index,:new和:create actions适用于学生。

为什么我收到/students/3的路由错误,而不是students#show中指示的路由到rake routes

/students/3上的

例外:

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

Try running rake routes for more information on available routes.

以下是相关位......

rake路由输出:

...
            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
...

的routes.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
...

控制器和视图

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

我正在cancan使用authlogic,但从未遇到过这些路由错误。

1 个答案:

答案 0 :(得分: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>