0

我正在尝试使用此 url 访问我的 checkpoints#index 方法: http://localhost:3000/lessons/introductions/checkpoints/

但我得到这个错误:

No route matches {:action=>"show", :controller=>"checkpoints", :lesson_id=>#<Checkpoint id: 1, checkpoint: "Definition of Monetary Policy", url: "http://www.youtube.com/watch?v=HX3wOWN9-sM", objective: "Define monetary policy", description: "Welcome to this series of lectures on monetary poli...", question: "What is the definition of monetary policy?", hint: "It is the deliberate manipulation of something OR s...", answer: "-", lesson_id: 8, created_at: "2012-06-29 07:21:47", updated_at: "2012-06-30 08:24:15", slug: "definition-of-monetary-policy">}

这不应该是这种情况,因为在我的“rake routes”中:

      lesson_checkpoints GET    /lessons/:lesson_id/checkpoints(.:format)          checkpoints#index
                         POST   /lessons/:lesson_id/checkpoints(.:format)          checkpoints#create
   new_lesson_checkpoint GET    /lessons/:lesson_id/checkpoints/new(.:format)      checkpoints#new
  edit_lesson_checkpoint GET    /lessons/:lesson_id/checkpoints/:id/edit(.:format) checkpoints#edit
       lesson_checkpoint GET    /lessons/:lesson_id/checkpoints/:id(.:format)      checkpoints#show
                         PUT    /lessons/:lesson_id/checkpoints/:id(.:format)      checkpoints#update
                         DELETE /lessons/:lesson_id/checkpoints/:id(.:format)      checkpoints#destroy`

它不会加载#index 操作而不是#show 操作吗?有谁知道这个错误的可能原因?

这就是我的 routes.rb 的样子:

resources :lessons do
  resources :checkpoints
end  

resources :lessons

resources :topics do
  resources :lessons
end 

#Devise Routes
devise_for :users


resources :subjects do
  resources :topics
end
4

3 回答 3

0

我认为您没有提到“介绍”的路线。

resources :lessons do
   resources :introductions
end 

resources :introductions do
   resources :checkpoints
end
于 2012-06-30T10:01:21.637 回答
0

我看到两个问题:

  1. 我认为为您的课程模型设置friendly_id 存在一些问题。您的friendly_id 设置将Checkpoint 对象作为您的课程的友好ID,而不是课程的标题,这是您从给定的URL 中想要的。

  2. 您的路线声明不正确。您两次声明资源“课程”。

resources :lessons do  
  resources :checkpoints  
end  
resources :lessons  

您可以在控制台中检查您的friendly_id 的正确性:

lesson = Lesson.create(name: 'introduction')
lesson.friendly_id #=> should output 'introduction'
于 2012-07-01T07:20:54.093 回答
0

我发现了问题:我的friendly_id 是正确的。在我看来,错误本身就是错误的。我已经定义了两次资源,所以我可以访问 localhost/lessons/blahblah 的 url。这对我有用。我的视图文件需要编辑(从默认的未嵌套文件开始),一旦更正,一切都会再次顺利进行。

于 2012-07-01T08:17:33.000 回答