0

作为背景,我目前有三个模型,SchoolCourseSection它们都是一对多的关系(学校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        /   
4

2 回答 2

1

由于您的课程与学校嵌套,请试试这个

你的模型应该有

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

您可以通过运行来了解有关此路由的更多信息

rake routes

高温高压

于 2013-01-14T03:14:25.257 回答
1

您需要在新部分请求中包含这些参数

{:School_id=> some_id, :course_id=>some_id}

这样您就可以将它们与课程部分绑定

在节控制器

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

希望这会治愈:)

于 2013-01-15T08:29:00.633 回答