readings_controller.rb
2.16 KB
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
include OwnershipAuthentication
class ReadingsController < ApplicationController
before_action :authenticate_user!, except: [:index]
before_action :reading_owner?, except: [:new, :create]
before_action :reading_group_owner?, only: [:new, :create]
before_action :set_reading, only: [:edit, :update, :destroy]
def new
@reading_group_id = params[:reading_group_id]
@reading = Reading.new
end
def create
@reading = Reading.new(reading_params)
@reading.group_id = params[:reading_group_id].to_i
respond_to do |format|
create_and_redir(format)
end
end
# GET /readings/1/edit
def edit
@reading_group_id = params[:reading_group_id]
end
# PUT /reading_groups/1/readings/1
# PUT /reading_groups/1/readings/1.json
def update
@reading.group_id = params[:reading_group_id].to_i
respond_to do |format|
if @reading.update(reading_params)
format.html { redirect_to(reading_group_path(params[:reading_group_id].to_i), notice: 'Reading was successfully updated.') }
format.json { head :no_content }
else
failed_action(format, 'edit')
end
end
end
# DELETE /reading_groups/1/readings/1
# DELETE /reading_groups/1/readings/1
def destroy
@reading.destroy
respond_to do |format|
format.html { redirect_to reading_group_path(params[:reading_group_id].to_i) }
format.json { head :no_content }
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def reading_params
params[:reading]
end
# Duplicated code on create and update actions extracted here
def failed_action(format, destiny_action)
@reading_group_id = params[:reading_group_id]
format.html { render action: destiny_action }
format.json { render json: @reading.errors, status: :unprocessable_entity }
end
# Code extracted from create action
def create_and_redir(format)
if @reading.save
format.html { redirect_to reading_group_path(@reading.group_id), notice: 'Reading was successfully created.' }
else
failed_action(format, 'new')
end
end
def set_reading
@reading = Reading.find(params[:id].to_i)
end
end