courses_controller.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # @author Dumitru Ursu
  2. # Controller for the courses
  3. class CoursesController < ApplicationController
  4. before_action :set_course, only: [:show, :run, :edit, :update, :destroy]
  5. before_action :authenticate_user!, except: [:show, :index]
  6. before_action :redirect_to_course, except: :run
  7. def index
  8. @courses = Course.all
  9. end
  10. def show
  11. end
  12. def run
  13. @slides_path = "#{Rails.root}" + '/public' + @course.slides.url.sub!(/\?(.*)\z/, '')
  14. end
  15. def new
  16. @course = Course.new
  17. end
  18. def edit
  19. end
  20. def create
  21. @course = current_user.courses.new(course_params)
  22. if @course.save
  23. redirect_to @course, notice: 'Course was successfully created.'
  24. else
  25. render action: 'new'
  26. end
  27. end
  28. def update
  29. if @course.update(course_params)
  30. redirect_to @course, notice: 'Course was successfully updated.'
  31. else
  32. render action: 'edit'
  33. end
  34. end
  35. def destroy
  36. @course.destroy
  37. redirect_to courses_url
  38. end
  39. private
  40. # Use callbacks to share common setup or constraints between actions.
  41. def set_course
  42. @course = Course.find(params[:id])
  43. end
  44. def redirect_to_course
  45. course = current_user.nearest_course
  46. # we redirect to the course page even if it's 1.hour earlier
  47. # TODO add a setting in the user profile for time before redirection
  48. #redirect_to course_start_path(course) if Time.now + 1.hour > course.starts_at
  49. end
  50. def course_params
  51. params.require(:course).permit(:title, :description, :language, :slides,
  52. :prerequisites, :duration, :rating, :starts_at)
  53. end
  54. end