質問編集履歴

2

下部に、reception_paramsを追加いたいました。

2021/03/05 14:32

投稿

azumaken
azumaken

スコア14

test CHANGED
File without changes
test CHANGED
@@ -454,6 +454,16 @@
454
454
 
455
455
 
456
456
 
457
+ ```ここに言語を入力
458
+
459
+
460
+
461
+ def reception_params
462
+
463
+ params.permit(:date_of_use, :start_time, :end_time, :start_transfer, :end_transfer, :start_place, :end_place, :event, :remarks)
464
+
465
+ ```
466
+
457
467
  恐らく、他の方法があるのでは無いかと思いますが、知識不足ゆえに解決できません。
458
468
 
459
469
  お忙しい中、恐れ入りますが教えて下さい。

1

veiw,controller,logを追加致しました

2021/03/05 14:32

投稿

azumaken
azumaken

スコア14

test CHANGED
File without changes
test CHANGED
@@ -20,17 +20,387 @@
20
20
 
21
21
 
22
22
 
23
+ ```erb
24
+
25
+
26
+
27
+ 1ヶ月単位の予約ページ
28
+
29
+
30
+
31
+ <%= form_with(model: resource, url: month_update_users_reception_path(date: @first_day), method: :patch, local: true) do |f| %>
32
+
33
+ <table class="table table-bordered">
34
+
35
+ <thead>
36
+
37
+ <tr>
38
+
39
+ <th>申請</th>
40
+
41
+ <th>ご利用日</th>
42
+
43
+ <th style="width: 50px">曜日</th>
44
+
45
+ <th>迎え時間</th>
46
+
47
+ <th>迎え場所</th>
48
+
49
+ <th style="width: 100px"> 迎え有無</th>
50
+
51
+ <th>送り時間</th>
52
+
53
+ <th>送り場所</th>
54
+
55
+ <th style="width: 100px"> 送り有無</th>
56
+
57
+ <th style="width: 100px">イベント</th>
58
+
59
+ <th>備考</th>
60
+
61
+ </tr>
62
+
63
+ </thead>
64
+
65
+ <tbody>
66
+
67
+ <% @receptions.each do |day| %>
68
+
69
+ <%= f.fields_for "receptions[]", day do |r| %>
70
+
71
+ <% css_class =
72
+
73
+ case $days_of_the_week[day.date_of_use.wday]
74
+
75
+ when '土'
76
+
77
+ 'text-primary'
78
+
79
+ when '日'
80
+
81
+ 'text-danger'
82
+
83
+ end
84
+
85
+ %>
86
+
87
+ <tr>
88
+
89
+ <td><%= r.check_box :request, {checked: false, class: "sample"} %></td>
90
+
91
+ <td><%= l(day.date_of_use, format: :short) %></td>
92
+
93
+ <td class="<%= css_class %>"><%= $days_of_the_week[day.date_of_use.wday] %></td>
94
+
95
+ <td><%= r.time_field :start_time,{ max: "18:30", min: "08:30", class: "form-control input-sm" }%></td>
96
+
97
+ <td><%= r.select :start_place, [ ["自宅",1], ["学校 or 園", 2], ["祖父母宅", 3 ] ,["なし", 4]], {prompt: "迎え場所を選択"}, {class: "form-control input-sm" } %></td>
98
+
99
+ <td><%= r.check_box :start_transfer,{checked: true}, "true", "false" %></td>
100
+
101
+             ↑1ヶ月ページの問題となるカラム
102
+
103
+ <td><%= r.time_field :end_time,{ max: "18:30", min: "08:30", class: "form-control input-sm" } %></td>
104
+
105
+ <td><%= r.select :end_place, [ ["自宅",1], ["祖母宅", 2], ["その他", 3 ] ,["なし", 4]], {prompt: "送り場所を選択"}, {class: "form-control input-sm" } %></td>
106
+
107
+ <td><%= r.check_box :end_transfer,{checked: true}, "true", "false" %</td>
108
+
109
+ <td><%= r.check_box :event %></td>
110
+
111
+ <td><%= r.text_field :remarks, class: "form-control" %></td>
112
+
113
+ </tr>
114
+
115
+ <% end %>
116
+
117
+ <% end %>
118
+
119
+ </tbody>
120
+
121
+ </table>
122
+
123
+ <div class="table_modal">
124
+
125
+ <%= link_to "戻る", user_path(current_user), class: "btn btn-default" %>
126
+
127
+ <%= f.submit "上記の内容で受付を申請", class: "btn btn-primary" %>
128
+
129
+ </div>
130
+
131
+ <% end %>
132
+
133
+ ```
134
+
135
+
136
+
137
+ ```erb
138
+
139
+ <%= form_with( url: day_update_users_receptions_path, method: :patch, remote: true) do |f| %>
140
+
141
+ <table class="table table-bordered">
142
+
143
+ <thead class="table_modal">
144
+
145
+ <th>ご利用日</th>
146
+
147
+ <th>迎え時間</th>
148
+
149
+ <th>迎え場所</th>
150
+
151
+ <th style="width: 100px"> 迎え有無</th>
152
+
153
+ <th>送り時間</th>
154
+
155
+ <th>送り場所</th>
156
+
157
+ <th style="width: 100px"> 送り有無</th>
158
+
159
+ <th style="width: 100px">イベント</th>
160
+
161
+ <th>備考</th>
162
+
163
+ </thead>
164
+
165
+ <tbody class="table_modal">
166
+
167
+ <td><%= f.date_field :date_of_use,{value: "", class: "form-control input-sm" ,required: true } %></td>
168
+
169
+ <td><%= f.time_field :start_time ,{ max: "18:30", min: "08:30", class: "form-control input-sm" ,required: true } %></td>
170
+
171
+ <td><%= f.select :start_place, [ ["自宅",1], ["学校 or 園", 2], ["祖父母宅", 3 ] ,["なし", 4]], {prompt: "迎え場所を選択"}, {class: "form-control input-sm" ,required: true } %></td>
172
+
173
+ <td><%= f.check_box :start_transfer,{checked: true}, "true", "false" %></td>
174
+
175
+ ↑1ヶ月ページの問題となるカラムと同様(ここをfalseにすると1ヶ月ページに反映されない)
176
+
177
+ <td><%= f.time_field :end_time ,{ max: "18:30", min: "08:30", class: "form-control input-sm" ,required: true } %></td>
178
+
179
+ <td><%= f.select :end_place, [ ["自宅",1], ["祖母宅", 2], ["その他", 3 ] ,["なし", 4]], {prompt: "送り場所を選択"}, {class: "form-control input-sm" ,required: true } %></td>
180
+
181
+ <td><%= f.check_box :end_transfer,{checked: true}, "true", "false" %></td>
182
+
183
+ <td><%= f.check_box :event,{checked: false}, "true", "false" %></td>
184
+
185
+ <td><%= f.text_field :remarks, class: "form-control" %></td>
186
+
187
+ </tbody>
188
+
189
+ </table>
190
+
191
+ <div class="table_modal">
192
+
193
+ <%= f.submit "上記の内容で受付を申請", class: "btn btn-primary" %>
194
+
195
+ </div>
196
+
197
+ <% end %>
198
+
199
+
200
+
201
+ ```
202
+
203
+
204
+
23
205
  ```ここに言語を入力
24
206
 
25
207
 
26
208
 
27
- ヶ月ページの問題となるカラム
209
+ 日単位ページの処理
210
+
211
+
212
+
28
-
213
+ def day_edit
214
+
29
-
215
+ @user = User.find(current_user.id)
216
+
30
-
217
+ end
218
+
219
+
220
+
221
+ def day_update
222
+
223
+ @user = User.find(current_user.id)
224
+
225
+ @reception = @user.receptions.find_by(date_of_use: params[:date_of_use])
226
+
227
+ respond_to do |format|
228
+
229
+ if @reception.update(reception_params)
230
+
231
+ flash[:notice] = "指定日の受付に成功しました"
232
+
233
+ format.html { redirect_to @receprion }
234
+
31
- <td><%= r.check_box :start_transfer,{checked: true}, "true", "false" %></td>
235
+ format.js { render js: "window.location = '#{current_user.id}'" }
236
+
32
-
237
+ else
238
+
239
+ format.html { render :_new }
240
+
241
+ format.js { render :errors }
242
+
243
+ end
244
+
245
+ end
246
+
247
+ end
248
+
249
+
250
+
33
- ```
251
+ ```
252
+
253
+
254
+
255
+ ```
256
+
257
+
258
+
259
+
260
+
261
+ 1ヶ月単位ページの処理
262
+
263
+
264
+
265
+
266
+
267
+
268
+
269
+ def month_edit
270
+
271
+ @first_day = params[:date].nil? ?
272
+
273
+ Date.current.beginning_of_month : params[:date].to_date
274
+
275
+ @last_day = @first_day.end_of_month
276
+
277
+ one_month = [*@first_day..@last_day]
278
+
279
+ @receptions = current_user.receptions.where(date_of_use: @first_day..@last_day).order("date_of_use ASC")
280
+
281
+ unless one_month.count == @receptions.count
282
+
283
+ ActiveRecord::Base.transaction do
284
+
285
+ one_month.each { |day| current_user.receptions.create(date_of_use: day) }
286
+
287
+ end
288
+
289
+ @receptions = current_user.receptions.where(date_of_use: @first_day..@last_day).order("date_of_use ASC")
290
+
291
+ end
292
+
293
+
294
+
295
+ rescue ActiveRecord::RecordInvalid
296
+
297
+ flash[:danger] = "ページ情報の取得に失敗しました、再アクセスしてください。"
298
+
299
+ redirect_to root_url
300
+
301
+ end
302
+
303
+ @user = User.find(current_user.id)
304
+
305
+ @contact = @user.contact
306
+
307
+ @reception = @user.receptions.where.not(start_time: nil)
308
+
309
+ end
310
+
311
+
312
+
313
+ def month_update
314
+
315
+ @user = User.find(current_user.id)
316
+
317
+ ActiveRecord::Base.transaction do
318
+
319
+ # 成功の数を入れる変数
320
+
321
+ r1 = 0
322
+
323
+ month_params.each do |id,item|
324
+
325
+ reception = Reception.find(id)
326
+
327
+ # 条件1
328
+
329
+ if item[:request] == "1"
330
+
331
+ # 条件2
332
+
333
+ if item[:start_time].blank? && item[:end_time].blank?
334
+
335
+ flash[:alert] = "空白の時間が見つかりました。"
336
+
337
+ redirect_to @user and return
338
+
339
+ elsif
340
+
341
+ # 条件3
342
+
343
+ item[:start_place].blank? && item[:end_place].blank?
344
+
345
+ flash[:alert] = "空白の場所が見つかりました。"
346
+
347
+ redirect_to @user and return
348
+
349
+ elsif
350
+
351
+ # 条件4
352
+
353
+ item[:start_time] >= item[:end_time]
354
+
355
+ flash[:alert] = "設定時間に誤りがあります"
356
+
357
+ redirect_to @user and return
358
+
359
+ end
360
+
361
+ r1 += 1
362
+
363
+ reception.update!(item)
364
+
365
+
366
+
367
+ end
368
+
369
+ end
370
+
371
+ if r1 > 0
372
+
373
+ flash[:notice] = "受付を#{r1}件申請しました"
374
+
375
+ redirect_to @user
376
+
377
+ else
378
+
379
+ flash[:alert] = "「申請」にチェックを入れて下さい。"
380
+
381
+ redirect_to @user and return
382
+
383
+ end
384
+
385
+ end
386
+
387
+ rescue ActiveRecord::RecordInvalid
388
+
389
+ flash[:alert] = "無効な入力データがあった為、更新をキャンセルしました。"
390
+
391
+ edirect_to redirect_to @user
392
+
393
+ end
394
+
395
+
396
+
397
+ ```
398
+
399
+
400
+
401
+
402
+
403
+
34
404
 
35
405
 
36
406
 
@@ -40,12 +410,46 @@
40
410
 
41
411
 
42
412
 
43
- UPDATE "receptions" SET "start_time" = ?, "end_time" = ?, "start_transfer" = ?, "end_transfer" = ?, "start_place" = ?, "end_place" = ?, "event" = ?, "remarks" = ?, "updated_at" = ? WHERE "receptions"."id" = ? [["start_time", "2000-01-01 12:00:00"], ["end_time", "2000-01-01 13:00:00"], ["start_transfer", 1],
44
-
45
- ["end_transfer", 0], ["start_place", "4"], ["end_place", "4"], ["event", 0], ["remarks", ""], ["updated_at", "2021-03-04 11:27:49.286850"], ["id", 62]]
413
+ Started PATCH "/users/receptions/day_update" for 127.0.0.1 at 2021-03-04 23:37:50 +0900
414
+
415
+ Processing by Users::ReceptionsController#day_update as JS
416
+
417
+ Parameters: {"utf8"=>"✓", "authenticity_token"=>"vFfWzg/NLsADn8C6dhokiUbeDluGP2+Dd2fp6vyqugU/cvkiWJnNHzIlAypJte/ecupIltmyz3BFCf+Za058ew==", "date_of_use"=>"2021-03-24", "start_time"=>"09:00", "start_place"=>"1",
418
+
419
+ "start_transfer"=>"false", "end_time"=>"12:00", "end_place"=>"1", "end_transfer"=>"true", "event"=>"false", "remarks"=>"", "commit"=>"上記の内容で受付を申請"}
46
420
 
47
421
  ↑こちらのカラムです
48
422
 
423
+ User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 4], ["LIMIT", 1]]
424
+
425
+ ↳ app/controllers/users/receptions_controller.rb:10
426
+
427
+ User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 4], ["LIMIT", 1]]
428
+
429
+ ↳ app/controllers/users/receptions_controller.rb:10
430
+
431
+ Reception Load (0.8ms) SELECT "receptions".* FROM "receptions" WHERE "receptions"."user_id" = ? AND "receptions"."date_of_use" = ? LIMIT ? [["user_id", 4], ["date_of_use", "2021-03-24"], ["LIMIT", 1]]
432
+
433
+ ↳ app/controllers/users/receptions_controller.rb:11
434
+
435
+ Unpermitted parameters: :utf8, :_method, :authenticity_token, :commit
436
+
437
+ (0.1ms) begin transaction
438
+
439
+ ↳ app/controllers/users/receptions_controller.rb:13
440
+
441
+ Reception Update (0.6ms) UPDATE "receptions" SET "start_time" = ?, "end_time" = ?, "start_transfer" = ?, "end_transfer" = ?, "start_place" = ?, "end_place" = ?, "event" = ?, "remarks" = ?, "updated_at" = ? WHERE "receptions"."id" = ? [["start_time", "2000-01-01 09:00:00"], ["end_time", "2000-01-01 12:00:00"], ["start_transfer", 0], ["end_transfer", 1], ["start_place", "1"], ["end_place", "1"], ["event", 0], ["remarks", ""], ["updated_at", "2021-03-04 23:37:50.163148"], ["id", 24]]
442
+
443
+ ↳ app/controllers/users/receptions_controller.rb:13
444
+
445
+ (1.0ms) commit transaction
446
+
447
+ ↳ app/controllers/users/receptions_controller.rb:13
448
+
449
+ Completed 200 OK in 43ms (Views: 0.2ms | ActiveRecord: 4.2ms)
450
+
451
+ ↑こちらのカラムです
452
+
49
453
  ```
50
454
 
51
455