作为背景,我目前有三个模型,School
和Course
,Section
它们都是一对多的关系(学校has_many
课程和课程has_many
部分,对应belongs_to
关系也建立在模型中)。我还有以下资源(稍后会设置排除项):
resources :schools do
resources :courses
end
resources :sections #not part of the nest
虽然sections
可以作为嵌套资源的一部分工作,但我保留了它,因为Rails 指南强烈建议嵌套只有一层深。
所以,我的麻烦在于创建一个新部分(In SectionsController
),并通过course_id
def new
@course = Course.find(params[:id]) #this line results in an error
@section = @course.sections.new
end
第一行总是会引发“找不到没有 ID 的课程”错误,尽管尝试了使用 :id、:course_id 等的各种不同组合,但我无法通过该错误。由于Course
是嵌套资源,是否存在我还缺少什么?谢谢你的帮助!
运行时rake routes
,输出如下:
sections GET /sections(.:format) sections#index
POST /sections(.:format) sections#create
new_section GET /sections/new(.:format) sections#new
edit_section GET /sections/:id/edit(.:format) sections#edit
section GET /sections/:id(.:format) sections#show
PUT /sections/:id(.:format) sections#update
DELETE /sections/:id(.:format) sections#destroy
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_school_course GET /schools/:school_id/courses/:id/edit(.:format) courses#edit
school_course GET /schools/:school_id/courses/:id(.:format) courses#show
PUT /schools/:school_id/courses/:id(.:format) courses#update
DELETE /schools/:school_id/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
root /