schools_controller.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. class SchoolsController < ApplicationController
  2. before_action :set_school, only: [:show, :edit, :update, :destroy]
  3. before_action :check_admin, only: [:edit, :update]
  4. before_action :check_global_admin, only: [:new, :create]
  5. def show
  6. end
  7. def new
  8. @school = School.new
  9. end
  10. def edit
  11. end
  12. def create
  13. @school = School.new(school_params)
  14. if @school.save
  15. flash[:success] = "Created school"
  16. redirect_to @school
  17. else
  18. render :new
  19. end
  20. end
  21. def update
  22. if @school.update(school_params)
  23. flash[:success] = "Updated school"
  24. redirect_to @school
  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_school
  32. @school = School.find(params[:id])
  33. end
  34. def school_params
  35. params.require(:school).permit(:name)
  36. end
  37. def check_admin
  38. unless current_user.is_administrator_at?(@school) || current_user.admin?
  39. redirect_to root_url
  40. end
  41. end
  42. def check_global_admin
  43. unless current_user.admin?
  44. redirect_to root_url
  45. end
  46. end
  47. end