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.

Reply
Join the discussion
Create an account Log in

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

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

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