courses_controller.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. class CoursesController < ApplicationController
  2. before_action :set_course, only: [:show, :edit, :update, :destroy]
  3. before_action :authenticate_user!, except: [:show, :index]
  4. def index
  5. @courses = Course.all
  6. end
  7. def show
  8. end
  9. def new
  10. @course = Course.new
  11. end
  12. def edit
  13. end
  14. def create
  15. @course = Course.new(course_params)
  16. @course.user_id = current_user.id
  17. if @course.save
  18. redirect_to @course, notice: 'Course was successfully created.'
  19. else
  20. render action: 'new'
  21. end
  22. end
  23. def update
  24. if @course.update(course_params)
  25. redirect_to @course, notice: 'Course was successfully updated.'
  26. else
  27. render action: 'edit'
  28. end
  29. end
  30. def destroy
  31. @course.destroy
  32. redirect_to courses_url
  33. end
  34. private
  35. # Use callbacks to share common setup or constraints between actions.
  36. def set_course
  37. @course = Course.find(params[:id])
  38. end
  39. # Never trust parameters from the scary internet, only allow the white list through.
  40. def course_params
  41. params.require(:course).permit(:title, :description, :language, :prerequisites, :duration, :rating)
  42. end
  43. end