All threads / How do i make a Courses - Chapter - Lesson Nesting!?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do i make a Courses - Chapter - Lesson Nesting!?

Corlett Nils asked in Rails

Hi all,

im new to Ruby on Rails and new to GoRails. I try to connect 3 tables.

My Models:

Course.rb:
class Course < ApplicationRecord
has_many :chapters
has_many :lessons, through: :chapters
end

Chapter.rb
class Chapter < ApplicationRecord
belongs_to :course
belongs_to :lesson

end

Lesson.rb
class Lesson < ApplicationRecord
has_many :chapters
has_many :courses, through: :chapters
end

My Course Controller looks like this:

class CourseController < ApplicationController
def index
end

def show

@course = Course.find(params[:id])
@chapters = @course.chapters
@lessons = @course.lessons



end

end

and show.html.erb
<% @chapters.each do |c| %>
<%= c.chapter %>
<%= link_to "lektionen", c %>
<% end %>

I can see a list of my courses. Thats working. Also i can see the chapters.

Chapter Controller:
class ChapterController < ApplicationController
def index
end

def show
@chapters = Chapter.find(params[:id])
@lessons = @chapters.lessons
end
end

i want to forward the id of the course so that chapters << lessons are the children of courses.

  • courses -- chapters --- lessons

But im not in able to get this working.

I hope yo can help me.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 46,687+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.