问题描述
作为背景,我目前有三个模型,School
、Course
和 Section
,它们都是一对多的关系(学校has_many
课程和课程has_many
部分,相应的belongs_to
关系也在模型中建立).我还有以下资源(排除项稍后设置):
As background, I currently have three models, School
, Course
and Section
, where they are all in a one-to-many relationships (School has_many
courses, and Course has_many
sections, with the corresponding belongs_to
relationships also established in the model). I also have the following resources (exclusions to be set later):
resources :schools do
resources :courses
end
resources :sections #not part of the nest
尽管 sections
可以作为嵌套资源的一部分工作,但自从 Rails guide 强烈建议嵌套仅一层深.
Although sections
could work as part of the nested resources, I kept it out since the Rails guide strongly recommended nests only one layer deep.
所以,我的麻烦在于创建一个新部分(在 SectionsController
中),并通过 course_id
So, my trouble is when it comes to creating a new section (In SectionsController
), and having it linked to the course via the 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
是一个嵌套资源,我还缺少其他什么东西吗?感谢您的帮助!
The first line would always raise an "Couldn't find Course without an ID" error, which I can't get past, despite trying various different combinations of using :id, :course_id, etc. Since Course
is a nested resource, is there something else that I'm missing? Thanks for your help!
运行 rake routes
时,输出如下:
When running rake routes
, here is the output:
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 /
推荐答案
您需要在新的部分请求中包含这些参数
you need to have these params in your new section request
{:School_id=> some_id, :course_id=>some_id}
这样你就可以让他们与课程绑定
So that you can get them section binding with course
在节控制器
def new
@school = School.find(params[:school_id])
@course = @school.courses.where(:id=>params[:course_id]).first
@section = @course.sections.new
end
希望这会治愈:)
这篇关于不断得到“无法找到没有 ID 的 Model_name";编写“新"方法时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!