/
courses_controller.rb
85 lines (73 loc) · 1.96 KB
/
courses_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class CoursesController < ApplicationController
# GET /courses
# GET /courses.json
def index
@courses = Course.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @courses }
end
end
# GET /courses/1
# GET /courses/1.json
def show
@course = Course.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @course }
end
end
# GET /courses/new
# GET /courses/new.json
def new
@course = Course.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @course }
end
end
# GET /courses/1/edit
def edit
@course = Course.find(params[:id])
end
# POST /courses
# POST /courses.json
def create
@course = Course.new(params[:course])
@course.owner_id = current_user.id
respond_to do |format|
if @course.save
format.html { redirect_to @course, notice: 'Course was successfully created.' }
format.json { render json: @course, status: :created, location: @course }
else
format.html { render action: "new" }
format.json { render json: @course.errors, status: :unprocessable_entity }
end
end
end
# PUT /courses/1
# PUT /courses/1.json
def update
@course = Course.find(params[:id])
@course.owner_id = current_user.id
respond_to do |format|
if @course.update_attributes(params[:course])
format.html { redirect_to @course, notice: 'Course was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @course.errors, status: :unprocessable_entity }
end
end
end
# DELETE /courses/1
# DELETE /courses/1.json
def destroy
@course = Course.find(params[:id])
@course.destroy
respond_to do |format|
format.html { redirect_to courses_url }
format.json { head :ok }
end
end
end