Skip to content

Commit

Permalink
Nest lessons in Courses and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dalvarez2596 committed Oct 28, 2024
1 parent 7daa3c4 commit 0849a06
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 22 deletions.
11 changes: 7 additions & 4 deletions app/controllers/lessons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def show

def new
@lesson = Lesson.new
@course = Course.friendly.find(params[:course_id])
end

def edit
Expand All @@ -19,10 +20,11 @@ def edit

def create
@lesson = Lesson.new(lesson_params)

@course = Course.friendly.find(params[:course_id])
@lesson.course_id = @course.id
respond_to do |format|
if @lesson.save
format.html { redirect_to @lesson, notice: "Lesson was successfully created." }
format.html { redirect_to [ @course, @lesson ], notice: "Lesson was successfully created." }
format.json { render :show, status: :created, location: @lesson }
else
format.html { render :new, status: :unprocessable_entity }
Expand All @@ -35,7 +37,7 @@ def update
authorize @lesson
respond_to do |format|
if @lesson.update(lesson_params)
format.html { redirect_to @lesson, notice: "Lesson was successfully updated." }
format.html { redirect_to [ @course, @lesson ], notice: "Lesson was successfully updated." }
format.json { render :show, status: :ok, location: @lesson }
else
format.html { render :edit, status: :unprocessable_entity }
Expand All @@ -49,13 +51,14 @@ def destroy
@lesson.destroy!

respond_to do |format|
format.html { redirect_to lessons_path, status: :see_other, notice: "Lesson was successfully destroyed." }
format.html { redirect_to @course, status: :see_other, notice: "Lesson was successfully destroyed." }
format.json { head :no_content }
end
end

private
def set_lesson
@course = Course.friendly.find(params[:course_id])
@lesson = Lesson.friendly.find(params[:id])
end
def lesson_params
Expand Down
2 changes: 1 addition & 1 deletion app/views/courses/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
.col-lg-6
= render 'courses/course', course: @course
.col-lg-6
= link_to 'Add lesson', new_lesson_path(course_id: @course.id)
= link_to 'Add lesson', new_course_lesson_path(@course, @lesson)
- @lessons.each do |lesson|
= render 'lessons/lesson', lesson: lesson
4 changes: 2 additions & 2 deletions app/views/lessons/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-# frozen_string_literal: true
= simple_form_for(@lesson) do |f|
= simple_form_for([@course, @lesson]) do |f|
= f.error_notification
= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present?

Expand All @@ -10,7 +10,7 @@
-# como estaba
-# = f.association :course
-# Esto de abajo asigno el input del html por defecto si ya es un lesson o por params. as: :hidden hide the input
= f.input :course_id, input_html: {value: @lesson.course_id || params[:course_id]}, as: :hidden
-# = f.input :course_id, input_html: {value: @course.id || params[:course_id]}, as: :hidden
.form-actions
= f.button :submit
6 changes: 3 additions & 3 deletions app/views/lessons/_lesson.html.haml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
.card
.card-header
= link_to lesson.title, lesson
= link_to lesson.title, [@course, lesson]
- if policy(lesson).show?
.card-body
= lesson.content
= lesson.course
- if policy(lesson).edit?
.card-footer
= link_to 'Edit', edit_lesson_path(lesson), class: 'btn btn-sm btn-warning'
= link_to 'Destroy', lesson, data: { turbo_method: :delete, confirm: 'Are you sure' }, class: 'btn btn-sm btn-danger'
= link_to 'Edit', edit_course_lesson_path(@course, lesson), class: 'btn btn-sm btn-warning'
= link_to 'Destroy', [@course, lesson], data: { turbo_method: :delete, confirm: 'Are you sure' }, class: 'btn btn-sm btn-danger'
%p

4 changes: 2 additions & 2 deletions app/views/lessons/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

= render 'form'

= link_to 'Show', @lesson
= link_to 'Show', [@course, @lesson]
\|
= link_to 'Back', lessons_path
= link_to 'Back to course', course_path(@course)
8 changes: 4 additions & 4 deletions app/views/lessons/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
%td= lesson.title
%td= lesson.content
%td= lesson.course
%td= link_to 'Show', lesson
%td= link_to 'Edit', edit_lesson_path(lesson)
%td= link_to 'Destroy', lesson, method: :delete, data: { confirm: 'Are you sure?' }
%td= link_to 'Show', course_lesson_path(@course, lesson)
%td= link_to 'Edit', edit_course_lesson_path(@course, lesson)
%td= link_to 'Destroy', [@course, lesson], method: :delete, data: { confirm: 'Are you sure?', turbo_frame: "_top" }

%br

= link_to 'New Lesson', new_lesson_path
= link_to 'New Lesson', new_course_lesson_path
2 changes: 1 addition & 1 deletion app/views/lessons/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

= render 'form'

= link_to 'Back', lessons_path
= link_to 'Back', course_path(@course)
6 changes: 3 additions & 3 deletions app/views/lessons/show.html.haml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
= link_to 'Courses', courses_path
\/
= link_to @lesson.course.title, course_path(@lesson.course)
= link_to @lesson.course.title, course_path(@course)
\/
= link_to @lesson.title, lesson_path(@lesson)
= link_to @lesson.title, course_lesson_path(@course, @lesson)

= render 'lessons/lesson', lesson: @lesson

= link_to 'Back to course', course_path(@lesson.course)
= link_to 'Back to course', course_path(@course)
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Rails.application.routes.draw do
resources :lessons
devise_for :users
resources :courses
resources :courses do
resources :lessons
end
resources :users, only: [ :index, :edit, :show, :update ]
get "home/index"
get "home/activity"
Expand Down

0 comments on commit 0849a06

Please sign in to comment.