1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- class AssignmentsController < ApplicationController
- before_action :set_assignment, only: [:show, :edit, :update, :destroy]
- before_action :check_permissions, only: [:edit, :update]
-
-
- def index
- @assignments = current_user.assignments
- end
-
-
- def show
- end
-
- def new
- @assignment = Assignment.new
- end
-
- def edit
- end
-
-
- def create
- @assignment = Assignment.new(assignment_params)
- check_permissions
- respond_to do |format|
- if @assignment.save
- flash[:success] = 'Assignment was successfully created.'
- format.html { redirect_to @assignment }
- format.json { render :show, status: :created, location: @assignment }
- else
- format.html { render :new }
- format.json { render json: @assignment.errors, status: :unprocessable_entity }
- end
- end
- end
-
-
- def update
- respond_to do |format|
- if @assignment.update(assignment_params)
- flash[:success] = 'Assignment was successfully updated.'
- format.html { redirect_to @assignment }
- format.json { render :show, status: :ok, location: @assignment }
- else
- format.html { render :edit }
- format.json { render json: @assignment.errors, status: :unprocessable_entity }
- end
- end
- end
-
-
- def destroy
- @assignment.destroy
- respond_to do |format|
- flash[:success] = 'Assignment was successfully destroyed.'
- format.html { redirect_to assignments_url }
- format.json { head :no_content }
- end
- end
- private
-
- def set_assignment
- @assignment = Assignment.find(params[:id])
- end
-
- def assignment_params
- allowed = [:name, :description, :due_at]
-
- allowed += [:course_id] if action_name == 'create'
- params.require(:assignment).permit(*allowed)
- end
- def check_permissions
- redirect_to root_url unless current_user.is_course_teacher?(@assignment.course)
- end
- end
|