Corlett Nils

Joined

50 Experience
0 Lessons Completed
0 Questions Solved

Activity

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.

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.