11class Teacher ::AssignmentsController < ApplicationController
2- before_action :set_course
3- before_action :set_assignment , only : %i[ show edit update destroy ]
2+ expose :teacher , :current_teacher
3+ expose :course
4+ expose :assignment , parent : :course
5+ expose :assignments , from : :course
46 before_action :ensure_teacher_teaches_course
57
6- def index
7- @assignments = @course . assignments . all
8- end
9-
10- def new
11- @assignment = @course . assignments . build
12- end
138
149 def create
15- @assignment = @course . assignments . create ( assignment_params )
16-
1710 respond_to do |format |
18- if @ assignment. save
11+ if assignment . save
1912 format . html { redirect_to teacher_course_assignments_path , notice : t ( ".notice" ) }
20- format . json { render :show , status : :created , location : @ assignment }
13+ format . json { render :show , status : :created , location : assignment }
2114 else
2215 format . html { render :new , status : :unprocessable_entity }
23- format . json { render json : @ assignment. errors , status : :unprocessable_entity }
16+ format . json { render json : assignment . errors , status : :unprocessable_entity }
2417 end
2518 end
2619 end
2720
2821 def update
2922 respond_to do |format |
30- if @ assignment. update ( assignment_params )
31- format . html { redirect_to [ :teacher , @ course, @ assignment ] , notice : t ( ".notice" ) }
32- format . json { render :show , status : :ok , location : @ assignment }
23+ if assignment . update ( assignment_params )
24+ format . html { redirect_to [ :teacher , course , assignment ] , notice : t ( ".notice" ) }
25+ format . json { render :show , status : :ok , location : assignment }
3326 else
3427 format . html { render :edit , status : :unprocessable_entity }
35- format . json { render json : @ assignment. errors , status : :unprocessable_entity }
28+ format . json { render json : assignment . errors , status : :unprocessable_entity }
3629 end
3730 end
3831 end
3932
4033 def destroy
41- @ assignment. destroy!
34+ assignment . destroy!
4235
4336 respond_to do |format |
4437 format . html { redirect_to teacher_course_assignments_path , status : :see_other , notice : t ( ".notice" ) }
@@ -47,20 +40,12 @@ def destroy
4740 end
4841
4942 private
50- def set_course
51- @course = Course . find ( params [ :course_id ] )
52- end
53-
54- def set_assignment
55- @assignment = @course . assignments . find ( params [ :id ] )
56- end
57-
5843 def assignment_params
5944 params . require ( :assignment ) . permit ( :title , :content , :deadline )
6045 end
6146
6247 def ensure_teacher_teaches_course
63- unless @ course. teacher_id == current_teacher . id
48+ unless course . teacher_id == teacher . id
6449 redirect_to teacher_courses_path , notice : t ( ".notice" )
6550 end
6651 end
0 commit comments