Rails路由ActiveRecord :: RecordNotFound错误

时间:2013-03-14 05:19:53

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

我添加了一个新的控制器操作并添加了相应的路径

  def students
    @students = Swimming::Student.all
    render :json => @students
  end


  namespace :swimming do 
    resources :classschedules do
        get 'students', :action => 'students', :as => :students ,:on => :collection
      end 
  end

但是当我访问此页面时

http://localhost:3000/swimming/classschedules/students

我收到了这个错误

ActiveRecord::RecordNotFound in Swimming::ClassschedulesController#show

Couldn't find Swimming::Classschedule with id=students

看起来rails试图加载另一条路径

GET    /swimming/classschedules/:id(.:format)            swimming/classschedules#show

我附上所有相关路线

         swimming_classschedules GET    /swimming/classschedules(.:format)                swimming/classschedules#index
                                 POST   /swimming/classschedules(.:format)                swimming/classschedules#create
      new_swimming_classschedule GET    /swimming/classschedules/new(.:format)            swimming/classschedules#new
     edit_swimming_classschedule GET    /swimming/classschedules/:id/edit(.:format)       swimming/classschedules#edit
          swimming_classschedule GET    /swimming/classschedules/:id(.:format)            swimming/classschedules#show
                                 PUT    /swimming/classschedules/:id(.:format)            swimming/classschedules#update
                                 DELETE /swimming/classschedules/:id(.:format)            swimming/classschedules#destroy

    date_swimming_classschedules GET    /swimming/classschedules/date/:date(.:format)     swimming/classschedules#date
students_swimming_classschedules GET    /swimming/classschedules/students(.:format)       swimming/classschedules#students
editnote_swimming_classschedules POST   /swimming/classschedules/editnote/:date(.:format) swimming/classschedules#editnote
                                 GET    /swimming/classschedules(.:format)                swimming/classschedules#index
                                 POST   /swimming/classschedules(.:format)                swimming/classschedules#create
                                 GET    /swimming/classschedules/new(.:format)            swimming/classschedules#new
                                 GET    /swimming/classschedules/:id/edit(.:format)       swimming/classschedules#edit
                                 GET    /swimming/classschedules/:id(.:format)            swimming/classschedules#show
                                 PUT    /swimming/classschedules/:id(.:format)            swimming/classschedules#update
                                 DELETE /swimming/classschedules/:id(.:format)            swimming/classschedules#destroy

如何解决此问题?

更新 * 已修复 *

这个问题因为我有两个

  namespace :swimming do 
    resources :classschedules do

     end 

   end

在routes.rb

1 个答案:

答案 0 :(得分:0)

从评论中复制答案,以便从“未答复”过滤器中删除此问题:

  

这个问题因为我有两个

namespace :swimming do 
  resources :classschedules do

  end 
end
     

在routes.rb

〜每wwli

回答