Keeps getting “Cannot find Model_name without ID”

2019-08-30 10:26发布

问题:

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        /   

回答1:

since your courses are nested with schools, try this

your model should have

class School < ActiveRecord::base
  has_many :courses
end

class Course < ActiveRecord::base
  belongs_to :school
end


def new
  school = School.find(params[:school_id])
  @course = school.courses.new
  #your code 
end

you can get more idea about this routing by running

rake routes

HTH



回答2:

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

In section controller

 def new
    @school = School.find(params[:school_id])
    @course = @school.courses.where(:id=>params[:course_id]).first
    @section = @course.sections.new
  end

hope this will cure :)