Skip to content

Instantly share code, notes, and snippets.

@kenmazaika
Created June 30, 2016 01:25
Show Gist options
  • Save kenmazaika/91116f0182b698b9fbd292363f926e89 to your computer and use it in GitHub Desktop.
Save kenmazaika/91116f0182b698b9fbd292363f926e89 to your computer and use it in GitHub Desktop.
class Instructor::LessonsController < ApplicationController
before_action :authenticate_user!
before_action :require_authorized_for_current_section, only: [:new, :create]
before_action :require_authorized_for_current_lesson, only: [:update]
def new
@lesson = Lesson.new
end
def create
@lesson = current_section.lessons.create(lesson_params)
redirect_to instructor_course_path(current_section.course)
end
def update
current_lesson.update_attributes(lesson_params)
render text: 'updated!'
end
private
def require_authorized_for_current_lesson
if current_lesson.section.course.user != current_user
render text: 'Unauthorized', status: :unauthorized
end
end
def current_lesson
@current_lesson ||= Lesson.find(params[:id])
end
def require_authorized_for_current_section
if current_section.course.user != current_user
return render text: 'Unauthorized', status: :unauthorized
end
end
helper_method :current_section
def current_section
if params[:section_id].present?
@current_section ||= Section.find(params[:section_id])
else
current_lesson.section
end
end
def lesson_params
params.require(:lesson).permit(:title, :subtitle, :video, :row_order_position)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment