0

I regularly use shallow routes on nested resources, so thought I missed something simple, but don't see why I'm getting a routing error for students#show.

I have a School with two nested resources: Courses and Students. All actions work on Courses. The :index, :new and :create actions work for Students.

Why do I get a routing error for /students/3 instead of being routed to students#show as indicated in rake routes?

exception on /students/3 :

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

Try running rake routes for more information on available routes.

Here are the relevant bits...

rake routes output:

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

controllers and views

I inspected, but since this is a routing exception, I don't think this code is reached. If you tell me otherwise, I can add them.

libraries

I'm using cancan with authlogic, but have never experienced a routing error from these.

3
  • 1
    Off topic: don't use get (like get 'approve') for state-changing paths. It's a CSRF hole! You'd better think about post/put instead.
    – jdoe
    Dec 4, 2012 at 14:43
  • The fact that there's no :id=>3 in the error message is suspicious, and also explains the error (you don't have a route for /students), but not the cause. Possible route conflict? Do you have anything else that's for the student controller? Dec 4, 2012 at 14:45
  • @jdoe good catch. I'll fix that. @mike-campbell you noticed the clue pointing to #index.
    – Anson
    Dec 4, 2012 at 14:49

1 Answer 1

0

Yes, it was something simple. I made a wrong assumption that a routing error means the controller and views aren't reached....and this "genius" forgot the logs have the full stack trace:

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'

My breadcrumbs was attempting to construct a link to students#index which doesn't exist. The breadcrumbs try to handle this case (line 25) but the check fails for nested resources:

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.