Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道:路由“;课程“-&燃气轮机&引用;章节「-&燃气轮机&引用;教训”;_Ruby On Rails - Fatal编程技术网

Ruby on rails 轨道:路由“;课程“-&燃气轮机&引用;章节「-&燃气轮机&引用;教训”;

Ruby on rails 轨道:路由“;课程“-&燃气轮机&引用;章节「-&燃气轮机&引用;教训”;,ruby-on-rails,Ruby On Rails,我是RubyonRails新手。我尝试连接3个表 我的模型: Course.rb: class Course < ApplicationRecord has_many :chapters has_many :lessons, through: :chapters end 我的模式: ActiveRecord::Schema.define(version: 2021_04_11_090326) do create_table "chapters", force:

我是RubyonRails新手。我尝试连接3个表

我的模型:

Course.rb:

class Course < ApplicationRecord
has_many :chapters
has_many :lessons, through: :chapters
end
我的模式:

ActiveRecord::Schema.define(version: 2021_04_11_090326) do

  create_table "chapters", force: :cascade do |t|
    t.string "chapter"
    t.integer "course_id", null: false
    t.integer "lesson_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["course_id"], name: "index_chapters_on_course_id"
    t.index ["lesson_id"], name: "index_chapters_on_lesson_id"
  end

  create_table "courses", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "lessons", force: :cascade do |t|
    t.string "title"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  add_foreign_key "chapters", "courses"
  add_foreign_key "chapters", "lessons"
end
我的路线.rb

Rails.application.routes.draw do
  get 'chapter/index'
  get 'chapter/show'
  get 'course/index'
  get 'course/show'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  
  resources :course 
    resources :chapter
  resources :lessons
end
如果我点击“课程#展示”动作,我可以看到章节。如果我点击某一章节,我会出现以下错误:

#章节:0x000055f7aeefbe50的未定义方法“课程”

我只想展示属于特定章节且该章节与课程相关的课程

但是我不能让它工作


我希望你能帮助我。

我认为在这种情况下,你应该重新考虑你的数据模型:

完成课程->章节->课程

class Course < ApplicationRecord
  has_many :chapters # a course has many chapters. alright
end

class Chapter < ApplicationRecord
  belongs_to :course # a chapter always belongs to a course
  has_many :lessons # and has many lessons ! check!
end

class Lesson < ApplicationRecord
  belongs_to :chapter # a lesson belongs to a chapter
  
  # helper method if you want to access course directly from lesson
  def course
    @course ||= chapter&.course
  end
end
通过此设置,您可以使用良好的层次结构

绘制路线:

Rails.application.routes.draw do
  resources :courses
  resources :chapters
  resources :lessons
end
class CourseController < ApplicationController
  def index
    courses
  end

  def show
    course
  end

  protected
  # get all courses
  def courses
    @courses ||= Course.order(:name)
  end
  
  # find course by id
  def course
    @course ||= courses.find(params[:id])
  end
end
如果你想在url中建立层次结构,你可以做一些更高级的事情

Rails.application.routes.draw do
  # use this if you know what you are doing :D
  resources :courses do
    resources :chapters do
      resources :lessons
    end
  end
end
您的控制器:

Rails.application.routes.draw do
  resources :courses
  resources :chapters
  resources :lessons
end
class CourseController < ApplicationController
  def index
    courses
  end

  def show
    course
  end

  protected
  # get all courses
  def courses
    @courses ||= Course.order(:name)
  end
  
  # find course by id
  def course
    @course ||= courses.find(params[:id])
  end
end
N+1问题: 要避免呈现此层次结构时出现N+1问题,请更改控制器并将包含添加到查询中:

  def course
    @course ||= courses.includes(chapters: :lessons).find(params[:id])
  end

我希望这为您提供了一个干净的数据模型的良好开端。

您好,谢谢您的回复。但是我不知道你说的安装是什么意思。我应该把这个写进我的章控制器吗?例如:@lessource=Chapter.first.lessource?我已经尝试了这个方法,但是我遇到了另一个错误:未定义#的方法'each'。因为我只有一个目标,对吗?我如何展示所有课程,例如“展示动作”一章中的课程id 1?我根本不了解建模。一门课程有几章,然后每章都有多节课,这难道不是一种继承吗?在这种情况下,它完全是反向的。我同意@max。我用一个干净的数据模型添加了一个答案你好,西蒙,哇,非常感谢。我可以看出,我必须了解这一点。你能给我一个提示,让我更好地理解这一点吗?你看过指南了吗?您应该在rails教程中搜索一些“如何创建博客”
ActiveRecord::Schema.define(version: 2021_04_11_090326) do

  create_table "courses", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end


  create_table "chapters", force: :cascade do |t|
    t.string "title"
    t.integer "course_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["course_id"], name: "index_chapters_on_course_id"
  end

      
  create_table "lessons", force: :cascade do |t|
    t.string "title"
    t.integer "chapter_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["chapter_id"], name: "index_lessons_on_chapter_id"
  end

end
Rails.application.routes.draw do
  resources :courses
  resources :chapters
  resources :lessons
end
Rails.application.routes.draw do
  # use this if you know what you are doing :D
  resources :courses do
    resources :chapters do
      resources :lessons
    end
  end
end
class CourseController < ApplicationController
  def index
    courses
  end

  def show
    course
  end

  protected
  # get all courses
  def courses
    @courses ||= Course.order(:name)
  end
  
  # find course by id
  def course
    @course ||= courses.find(params[:id])
  end
end
<h1><%= @course.name %></h1>
<% @course.chapters.each do |chapter| %>
  <h2><%= chapter.title %></h2>
  <% chapter.lessons.each do |lesson| %>
    <%= link_to "Lesson: " + lesson.title, lesson_url(lesson) %>
  <% end %>
<% end %>
  def course
    @course ||= courses.includes(chapters: :lessons).find(params[:id])
  end