assignments_controller.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. class AssignmentsController < ApplicationController
  2. before_action :set_assignment, only: [:show, :edit, :update, :destroy]
  3. before_action :check_permissions, only: [:edit, :update]
  4. # GET /assignments
  5. # GET /assignments.json
  6. def index
  7. @assignments = current_user.assignments
  8. end
  9. # GET /assignments/1
  10. # GET /assignments/1.json
  11. def show
  12. end
  13. # GET /assignments/new
  14. def new
  15. @assignment = Assignment.new
  16. end
  17. # GET /assignments/1/edit
  18. def edit
  19. end
  20. # POST /assignments
  21. # POST /assignments.json
  22. def create
  23. @assignment = Assignment.new(assignment_params)
  24. check_permissions
  25. respond_to do |format|
  26. if @assignment.save
  27. flash[:success] = 'Assignment was successfully created.'
  28. format.html { redirect_to @assignment }
  29. format.json { render :show, status: :created, location: @assignment }
  30. else
  31. format.html { render :new }
  32. format.json { render json: @assignment.errors, status: :unprocessable_entity }
  33. end
  34. end
  35. end
  36. # PATCH/PUT /assignments/1
  37. # PATCH/PUT /assignments/1.json
  38. def update
  39. respond_to do |format|
  40. if @assignment.update(assignment_params)
  41. flash[:success] = 'Assignment was successfully updated.'
  42. format.html { redirect_to @assignment }
  43. format.json { render :show, status: :ok, location: @assignment }
  44. else
  45. format.html { render :edit }
  46. format.json { render json: @assignment.errors, status: :unprocessable_entity }
  47. end
  48. end
  49. end
  50. # DELETE /assignments/1
  51. # DELETE /assignments/1.json
  52. def destroy
  53. @assignment.destroy
  54. respond_to do |format|
  55. flash[:success] = 'Assignment was successfully destroyed.'
  56. format.html { redirect_to assignments_url }
  57. format.json { head :no_content }
  58. end
  59. end
  60. private
  61. # Use callbacks to share common setup or constraints between actions.
  62. def set_assignment
  63. @assignment = Assignment.find(params[:id])
  64. end
  65. # Never trust parameters from the scary internet, only allow the white list through.
  66. def assignment_params
  67. allowed = [:name, :description, :due_at]
  68. # Don't allow moving an assignment to a different course
  69. allowed += [:course_id] if action_name == 'create'
  70. params.require(:assignment).permit(*allowed)
  71. end
  72. def check_permissions
  73. redirect_to root_url unless current_user.is_course_teacher?(@assignment.course)
  74. end
  75. end