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