質問編集履歴
1
model Reservation と controller Reservations のcodeを追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -22,11 +22,11 @@
|
|
22
22
|
|
23
23
|
expect {
|
24
24
|
|
25
|
-
post "/reservations", params: { reservation: { day: "2021-0
|
25
|
+
post "/reservations", params: { reservation: { day: "2021-07-02",
|
26
|
-
|
26
|
+
|
27
|
-
time: "
|
27
|
+
time: "9:00",
|
28
|
-
|
28
|
+
|
29
|
-
start_time: "2021-0
|
29
|
+
start_time: "2021-07-02T09:00:00+09:00",
|
30
30
|
|
31
31
|
user_id: 3 } }
|
32
32
|
|
@@ -72,17 +72,17 @@
|
|
72
72
|
|
73
73
|
get "/users/#{other_user.id}/reserve"
|
74
74
|
|
75
|
-
get new_reservation_path, params: { day: "2021-0
|
75
|
+
get new_reservation_path, params: { day: "2021-07-02",
|
76
|
-
|
76
|
+
|
77
|
-
time: "
|
77
|
+
time: "9:00" }
|
78
78
|
|
79
79
|
expect {
|
80
80
|
|
81
|
-
post "/reservations", params: { reservation: { day: "2021-0
|
81
|
+
post "/reservations", params: { reservation: { day: "2021-07-02",
|
82
|
-
|
82
|
+
|
83
|
-
time: "
|
83
|
+
time: "9:00",
|
84
|
-
|
84
|
+
|
85
|
-
start_time: "2021-0
|
85
|
+
start_time: "2021-07-02T09:00:00+09:00",
|
86
86
|
|
87
87
|
user_id: 3 } }
|
88
88
|
|
@@ -100,113 +100,227 @@
|
|
100
100
|
|
101
101
|
```
|
102
102
|
|
103
|
+
app/models/reservation.rb
|
104
|
+
|
105
|
+
```
|
106
|
+
|
107
|
+
class Reservation < ApplicationRecord
|
108
|
+
|
109
|
+
belongs_to :user
|
110
|
+
|
111
|
+
belongs_to :crop
|
112
|
+
|
113
|
+
validates :user_id, presence: true
|
114
|
+
|
115
|
+
validates :day, presence: true
|
116
|
+
|
117
|
+
validates :time, presence: true
|
118
|
+
|
119
|
+
validates :start_time, presence: true, uniqueness: true
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
def self.reservations_after_one_month
|
124
|
+
|
125
|
+
# 今日から1ヶ月先までのデータを取得
|
126
|
+
|
127
|
+
reservations = Reservation.all.where("day >= ?", Date.current).where("day < ?", Date.current >> 1).order(day: :desc)
|
128
|
+
|
129
|
+
# 配列を作成し、データを格納
|
130
|
+
|
131
|
+
# DBアクセスを減らすために必要なデータを配列にデータを入れる
|
132
|
+
|
133
|
+
reservation_data = []
|
134
|
+
|
135
|
+
reservations.each do |reservation|
|
136
|
+
|
137
|
+
reservations_hash = {}
|
138
|
+
|
139
|
+
reservations_hash.merge!(day: reservation.day.strftime("%Y-%m-%d"), time: reservation.time)
|
140
|
+
|
141
|
+
reservation_data.push(reservations_hash)
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
reservation_data
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
150
|
+
|
151
|
+
```
|
152
|
+
|
153
|
+
app/controllers/reservations_controller.rb
|
154
|
+
|
155
|
+
```
|
156
|
+
|
157
|
+
class ReservationsController < ApplicationController
|
158
|
+
|
159
|
+
before_action :logged_in_user
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
def new
|
164
|
+
|
165
|
+
@reservation = Reservation.new
|
166
|
+
|
167
|
+
@day = params[:day]
|
168
|
+
|
169
|
+
@time = params[:time]
|
170
|
+
|
171
|
+
@start_time = DateTime.parse(@day + " " + @time + " " + "JST")
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
def show
|
178
|
+
|
179
|
+
@reservation = Reservation.find(params[:id])
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
def create
|
186
|
+
|
187
|
+
@reservation = Reservation.new(reservation_params)
|
188
|
+
|
189
|
+
if @reservation.save
|
190
|
+
|
191
|
+
flash[:success] = "ビデオ通話予約しました"
|
192
|
+
|
193
|
+
redirect_to reservation_path @reservation.id
|
194
|
+
|
195
|
+
else
|
196
|
+
|
197
|
+
render :new
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
private
|
206
|
+
|
207
|
+
def reservation_params
|
208
|
+
|
209
|
+
params.require(:reservation).permit(:day, :time, :user_id, :start_time)
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
```
|
216
|
+
|
103
217
|
ターミナル
|
104
218
|
|
105
219
|
```
|
106
220
|
|
107
|
-
Started GET "/reservations/new?day=2021-0
|
221
|
+
Started GET "/reservations/new?day=2021-07-02&time=9%3A00" for ::1 at 2021-06-30 20:03:46 +0900
|
108
222
|
|
109
223
|
Processing by ReservationsController#new as HTML
|
110
224
|
|
111
|
-
Parameters: {"day"=>"2021-0
|
225
|
+
Parameters: {"day"=>"2021-07-02", "time"=>"9:00"}
|
112
|
-
|
226
|
+
|
113
|
-
User Load (1.
|
227
|
+
User Load (1.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]]
|
114
|
-
|
228
|
+
|
115
|
-
↳ app/helpers/sessions_helper.rb:4
|
229
|
+
↳ app/helpers/sessions_helper.rb:43
|
116
230
|
|
117
231
|
Rendering reservations/new.html.erb within layouts/application
|
118
232
|
|
119
|
-
Rendered shared/_error_messages.html.erb (0.
|
233
|
+
Rendered shared/_error_messages.html.erb (0.4ms)
|
120
|
-
|
234
|
+
|
121
|
-
Rendered reservations/new.html.erb within layouts/application (4
|
235
|
+
Rendered reservations/new.html.erb within layouts/application (3.4ms)
|
122
|
-
|
236
|
+
|
123
|
-
Rendered layouts/_rails_default.html.erb (6.
|
237
|
+
Rendered layouts/_rails_default.html.erb (6.4ms)
|
124
238
|
|
125
239
|
Rendered layouts/_shim.html.erb (0.3ms)
|
126
240
|
|
127
|
-
Rendered layouts/_navigation.html.erb (
|
128
|
-
|
129
|
-
Rendered layouts/_search.html.erb (
|
241
|
+
Rendered layouts/_navigation.html.erb (1.1ms)
|
242
|
+
|
243
|
+
Rendered layouts/_search.html.erb (0.8ms)
|
244
|
+
|
245
|
+
Rendered layouts/_footer.html.erb (0.2ms)
|
246
|
+
|
247
|
+
Rendered layouts/_js.html.erb (4.9ms)
|
248
|
+
|
249
|
+
Completed 200 OK in 35ms (Views: 23.5ms | ActiveRecord: 4.2ms)
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
Started POST "/reservations" for ::1 at 2021-06-30 20:03:47 +0900
|
256
|
+
|
257
|
+
Processing by ReservationsController#create as HTML
|
258
|
+
|
259
|
+
Parameters: {"utf8"=>"✓", "authenticity_token"=>"rlG1WUVkXKQwavRqoJto72BhQdrBCEE/X4VxVPOqyVEy6XTXAjLRhr4EVo5ixdHVAls3MD98Phe64kCOu+Pxfg==", "reservation"=>{"day"=>"2021-07-02", "time"=>"9:00", "user_id"=>"3", "start_time"=>"2021-07-02T09:00:00+09:00"}, "commit"=>"登録する"}
|
260
|
+
|
261
|
+
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]]
|
262
|
+
|
263
|
+
↳ app/helpers/sessions_helper.rb:43
|
264
|
+
|
265
|
+
(0.2ms) BEGIN
|
266
|
+
|
267
|
+
↳ app/controllers/reservations_controller.rb:17
|
268
|
+
|
269
|
+
CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]]
|
270
|
+
|
271
|
+
↳ app/controllers/reservations_controller.rb:17
|
272
|
+
|
273
|
+
Reservation Exists (0.9ms) SELECT 1 AS one FROM "reservations" WHERE "reservations"."start_time" = $1 LIMIT $2 [["start_time", "2021-07-02 00:00:00"], ["LIMIT", 1]]
|
274
|
+
|
275
|
+
↳ app/controllers/reservations_controller.rb:17
|
276
|
+
|
277
|
+
Reservation Create (1.0ms) INSERT INTO "reservations" ("user_id", "day", "time", "created_at", "updated_at", "start_time") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["user_id", 3], ["day", "2021-07-02"], ["time", "9:00"], ["created_at", "2021-06-30 11:03:47.069175"], ["updated_at", "2021-06-30 11:03:47.069175"], ["start_time", "2021-07-02 00:00:00"]]
|
278
|
+
|
279
|
+
↳ app/controllers/reservations_controller.rb:17
|
280
|
+
|
281
|
+
(0.8ms) COMMIT
|
282
|
+
|
283
|
+
↳ app/controllers/reservations_controller.rb:17
|
284
|
+
|
285
|
+
Redirected to http://localhost:3000/reservations/1
|
286
|
+
|
287
|
+
Completed 302 Found in 13ms (ActiveRecord: 3.5ms)
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
Started GET "/reservations/1" for ::1 at 2021-06-30 20:03:47 +0900
|
294
|
+
|
295
|
+
Processing by ReservationsController#show as HTML
|
296
|
+
|
297
|
+
Parameters: {"id"=>"1"}
|
298
|
+
|
299
|
+
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]]
|
300
|
+
|
301
|
+
↳ app/helpers/sessions_helper.rb:43
|
302
|
+
|
303
|
+
Reservation Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
304
|
+
|
305
|
+
↳ app/controllers/reservations_controller.rb:12
|
306
|
+
|
307
|
+
Rendering reservations/show.html.erb within layouts/application
|
308
|
+
|
309
|
+
Rendered reservations/show.html.erb within layouts/application (0.5ms)
|
310
|
+
|
311
|
+
Rendered layouts/_rails_default.html.erb (5.2ms)
|
312
|
+
|
313
|
+
Rendered layouts/_shim.html.erb (0.3ms)
|
314
|
+
|
315
|
+
Rendered layouts/_navigation.html.erb (1.2ms)
|
316
|
+
|
317
|
+
Rendered layouts/_search.html.erb (1.1ms)
|
130
318
|
|
131
319
|
Rendered layouts/_footer.html.erb (0.3ms)
|
132
320
|
|
133
|
-
Rendered layouts/_js.html.erb (5.
|
134
|
-
|
135
|
-
Completed 200 OK in
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
Started POST "/reservations" for ::1 at 2021-06-29 20:06:03 +0900
|
142
|
-
|
143
|
-
Processing by ReservationsController#create as HTML
|
144
|
-
|
145
|
-
Parameters: {"utf8"=>"✓", "authenticity_token"=>"O7GFgyneW0b+z0JA0iQcRplavCtuMh6zTt2+Bh2ha+OnCUQNbojWZHCh4KQQeqV8+2DKwZBGYZuruo/cVehTzA==", "reservation"=>{"day"=>"2021-06-30", "time"=>"10:00", "user_id"=>"3", "start_time"=>"2021-06-30T10:00:00+09:00"}, "commit"=>"登録する"}
|
146
|
-
|
147
|
-
User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]]
|
148
|
-
|
149
|
-
↳ app/helpers/sessions_helper.rb:49
|
150
|
-
|
151
|
-
(0.2ms) BEGIN
|
152
|
-
|
153
|
-
↳ app/controllers/reservations_controller.rb:17
|
154
|
-
|
155
|
-
CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]]
|
156
|
-
|
157
|
-
↳ app/controllers/reservations_controller.rb:17
|
158
|
-
|
159
|
-
Reservation Exists (2.4ms) SELECT 1 AS one FROM "reservations" WHERE "reservations"."start_time" = $1 LIMIT $2 [["start_time", "2021-06-30 01:00:00"], ["LIMIT", 1]]
|
160
|
-
|
161
|
-
↳ app/controllers/reservations_controller.rb:17
|
162
|
-
|
163
|
-
Reservation Create (0.4ms) INSERT INTO "reservations" ("user_id", "day", "time", "created_at", "updated_at", "start_time") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["user_id", 3], ["day", "2021-06-30"], ["time", "10:00"], ["created_at", "2021-06-29 11:06:03.337854"], ["updated_at", "2021-06-29 11:06:03.337854"], ["start_time", "2021-06-30 01:00:00"]]
|
164
|
-
|
165
|
-
↳ app/controllers/reservations_controller.rb:17
|
166
|
-
|
167
|
-
(0.6ms) COMMIT
|
168
|
-
|
169
|
-
↳ app/controllers/reservations_controller.rb:17
|
170
|
-
|
171
|
-
Redirected to http://localhost:3000/reservations/7
|
172
|
-
|
173
|
-
Completed 302 Found in 14ms (ActiveRecord: 4.7ms)
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
Started GET "/reservations/7" for ::1 at 2021-06-29 20:06:03 +0900
|
180
|
-
|
181
|
-
Processing by ReservationsController#show as HTML
|
182
|
-
|
183
|
-
Parameters: {"id"=>"7"}
|
184
|
-
|
185
|
-
User Load (1.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]]
|
186
|
-
|
187
|
-
↳ app/helpers/sessions_helper.rb:49
|
188
|
-
|
189
|
-
Reservation Load (0.3ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."id" = $1 LIMIT $2 [["id", 7], ["LIMIT", 1]]
|
190
|
-
|
191
|
-
↳ app/controllers/reservations_controller.rb:12
|
192
|
-
|
193
|
-
Rendering reservations/show.html.erb within layouts/application
|
194
|
-
|
195
|
-
Rendered reservations/show.html.erb within layouts/application (0.4ms)
|
196
|
-
|
197
|
-
Rendered layouts/_rails_default.html.erb (6.7ms)
|
198
|
-
|
199
|
-
Rendered layouts/_shim.html.erb (0.3ms)
|
200
|
-
|
201
|
-
Rendered layouts/_navigation.html.erb (1.1ms)
|
202
|
-
|
203
|
-
Rendered layouts/_search.html.erb (0.7ms)
|
204
|
-
|
205
|
-
Rendered layouts/_footer.html.erb (0.3ms)
|
206
|
-
|
207
|
-
Rendered layouts/_js.html.erb (5.9ms)
|
208
|
-
|
209
|
-
Completed 200 OK in 27ms (Views: 21.9ms | ActiveRecord: 1.6ms)
|
321
|
+
Rendered layouts/_js.html.erb (5.5ms)
|
322
|
+
|
323
|
+
Completed 200 OK in 25ms (Views: 20.5ms | ActiveRecord: 0.5ms)
|
210
324
|
|
211
325
|
```
|
212
326
|
|