Skip to content

Commit

Permalink
Added policy to create
Browse files Browse the repository at this point in the history
  • Loading branch information
dalvarez2596 committed Oct 29, 2024
1 parent 0849a06 commit 30e5cc8
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion app/controllers/lessons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def create
@lesson = Lesson.new(lesson_params)
@course = Course.friendly.find(params[:course_id])
@lesson.course_id = @course.id
authorize @lesson
respond_to do |format|
if @lesson.save
format.html { redirect_to [ @course, @lesson ], notice: "Lesson was successfully created." }
Expand Down Expand Up @@ -62,6 +63,6 @@ def set_lesson
@lesson = Lesson.friendly.find(params[:id])
end
def lesson_params
params.require(:lesson).permit(:title, :content, :course_id)
params.require(:lesson).permit(:title, :content)
end
end
4 changes: 3 additions & 1 deletion app/policies/lesson_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Scope < ApplicationPolicy::Scope
end

def show?
@user.has_role?(:admin) || @record.course.user == @user.id
@user.has_role?(:admin) || @record.course.user_id == @user.id
end

def edit?
Expand All @@ -25,9 +25,11 @@ def update?
end

def new?
# @record.course.user_id == @user.id
end

def create?
@record.course.user_id == @user.id
end

def destroy?
Expand Down
6 changes: 4 additions & 2 deletions app/views/courses/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
.col-lg-6
= render 'courses/course', course: @course
.col-lg-6
= link_to 'Add lesson', new_course_lesson_path(@course, @lesson)
-if policy(@course).edit?
= link_to "Add lesson", new_course_lesson_path(@course, @lesson), class: "d-flex justify-content-center btn btn-primary"
%p
- @lessons.each do |lesson|
= render 'lessons/lesson', lesson: lesson
= render "lessons/lesson", lesson: lesson
3 changes: 2 additions & 1 deletion app/views/lessons/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
= f.input :title
= f.label :content
= f.rich_text_area :content
%small= f.error :content, class: "text-danger"
-# 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: @course.id || params[:course_id]}, as: :hidden
.form-actions
= f.button :submit
= f.button :submit, {class: "btn btn-success mb-2 mt-2"}
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', course_path(@course)
= link_to 'Back to course', course_path(@course)

0 comments on commit 30e5cc8

Please sign in to comment.