courses_controller.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. class CoursesController < ApplicationController
  2. before_action :set_course, only: [:show, :edit, :update, :destroy]
  3. before_action :check_can_edit, only: [:edit, :update]
  4. before_action :check_can_create, only: [:create]
  5. def show
  6. end
  7. def new
  8. @course = Course.new
  9. end
  10. def edit
  11. end
  12. def create
  13. @course = Course.new(course_params)
  14. if @course.save
  15. flash[:success] = "Created course"
  16. redirect_to @course
  17. else
  18. render :new
  19. end
  20. end
  21. def update
  22. if @course.update(course_params)
  23. flash[:success] = "Updated course"
  24. redirect_to @course
  25. else
  26. render :edit
  27. end
  28. end
  29. private
  30. # Use callbacks to share common setup or constraints between actions.
  31. def set_course
  32. @course = Course.find(params[:id])
  33. end
  34. def course_params
  35. params.require(:course).permit(:name, :school_id, :starts_on, :ends_on)
  36. end
  37. def check_can_create
  38. unless current_user.is_administrator_at?(School.find(course_params[:school_id])) || current_user.admin?
  39. redirect_to root_url
  40. end
  41. end
  42. def check_can_edit
  43. unless current_user.is_administrator_at?(@course.school) || current_user.admin?
  44. redirect_to root_url
  45. end
  46. end
  47. end