-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathactivities_controller.rb
84 lines (72 loc) · 2.36 KB
/
activities_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
class ActivitiesController < ApplicationController
#before_action :set_activity, only: %i[ show edit update destroy ]
skip_before_action :authenticate_user!, only: %i[index]
# GET /activities or /activities.json
def index
@activities = Activity.all
end
# GET /activities/1 or /activities/1.json
def show
@activity = Activity.find(params[:id])
end
# GET /activities/new
def new
@activity = Activity.new
end
# GET /activities/1/edit
def edit
@activity = Activity.find(params[:id])
end
def stats
@total_duration = 0
@total_calories = 0
for i in Activity.all
@total_duration += i.duration
@total_calories += i.calories
end
end
# POST /activities or /activities.json
def create
@activity = Activity.new(activity_params)
respond_to do |format|
if @activity.save
format.html { redirect_to activity_url(@activity), notice: "Activity was successfully created." }
format.json { render :show, status: :created, location: @activity }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @activity.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /activities/1 or /activities/1.json
def update
@activity = Activity.find(params[:id])
respond_to do |format|
if @activity.update(activity_params)
format.html { redirect_to activity_url(@activity), notice: "Activity was successfully updated." }
format.json { render :show, status: :ok, location: @activity }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @activity.errors, status: :unprocessable_entity }
end
end
end
# DELETE /activities/1 or /activities/1.json
def destroy
@activity = Activity.find(params[:id])
@activity.destroy
respond_to do |format|
format.html { redirect_to activities_url, notice: "Activity was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_activity
@activity = Activity.find(params[:id])
end
# Only allow a list of trusted parameters through.
def activity_params
params.require(:activity).permit(:title, :activity_type, :start, :duration, :calories)
end
end