質問編集履歴

8

修正

2019/06/25 05:55

投稿

ikt_erk122
ikt_erk122

スコア40

test CHANGED
File without changes
test CHANGED
@@ -514,7 +514,7 @@
514
514
 
515
515
  t.datetime "updated_at", null: false
516
516
 
517
- t.datetime "next_day"
517
+ t.boolean "next_day", default: false
518
518
 
519
519
  t.index ["user_id"], name: "index_attendances_on_user_id"
520
520
 

7

修正

2019/06/25 05:55

投稿

ikt_erk122
ikt_erk122

スコア40

test CHANGED
File without changes
test CHANGED
@@ -526,6 +526,8 @@
526
526
 
527
527
  ```ここに言語を入力
528
528
 
529
+ <tbody>
530
+
529
531
  <% @dates.each do |day| %>
530
532
 
531
533
  <%= fields_for "attendances[]", day do |af| %>
@@ -564,12 +566,28 @@
564
566
 
565
567
  </td>
566
568
 
569
+   <td>
570
+
571
+  <% if day.started_at.present? && day.finished_at.present? %>
572
+
573
+ <%= working_times(day.started_at, day.finished_at) %>
574
+
575
+ <% seconds = (day.finished_at - day.started_at).to_i %>
576
+
577
+ <% @total_seconds = @total_seconds.to_i + seconds %>
578
+
579
+ <% end %>
580
+
581
+ </td>
582
+
567
583
  </tr>
568
584
 
569
585
  <% end %>
570
586
 
571
587
  <% end %>
572
588
 
589
+ </tbody>
590
+
573
591
  ```
574
592
 
575
593
  ・見本(編集前)

6

修正

2019/06/25 02:58

投稿

ikt_erk122
ikt_erk122

スコア40

test CHANGED
File without changes
test CHANGED
@@ -188,7 +188,11 @@
188
188
 
189
189
  redirect_to user_path(@user, params:{first_day: params[:date]})
190
190
 
191
+ if params[:next_day] == true
192
+
191
- @tomorrow = Time.now.tomorrow ←追加
193
+ Time.now.tomorrow
194
+
195
+ end
192
196
 
193
197
  else
194
198
 

5

修正

2019/06/24 06:09

投稿

ikt_erk122
ikt_erk122

スコア40

test CHANGED
File without changes
test CHANGED
@@ -568,4 +568,10 @@
568
568
 
569
569
  ```
570
570
 
571
+ ・見本(編集前)
572
+
571
- ![イメージ説明](a79515b419c9efc3d4eb9c6fd133cf46.png)
573
+ ![イメージ説明](aa6d4b40ec3ef29993ecea68f4ac53bd.png)
574
+
575
+ ・見本(編集後)
576
+
577
+ ![イメージ説明](99ef552b3582f8e6bc1c02086eacf3c6.png)

4

修正

2019/06/24 03:06

投稿

ikt_erk122
ikt_erk122

スコア40

test CHANGED
File without changes
test CHANGED
@@ -12,12 +12,6 @@
12
12
 
13
13
 
14
14
 
15
- 【質問】
16
-
17
- 丸投げになって申し訳ないですがアドバイスいただけると幸いです。よろしくお願いします。
18
-
19
-
20
-
21
15
  ・attendances/edit.html.erb
22
16
 
23
17
  ```ここに言語を入力
@@ -316,15 +310,7 @@
316
310
 
317
311
  class UsersController < ApplicationController
318
312
 
319
- before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :show]
313
+
320
-
321
- before_action :correct_user, only: [:edit, :update]
322
-
323
- before_action :admin_user, only: [:destroy, :edit_basic_info, :update_basic_info, :index]
324
-
325
- before_action :general_user, only: :show
326
-
327
- before_action :hidden, only: :show
328
314
 
329
315
 
330
316
 
@@ -496,157 +482,7 @@
496
482
 
497
483
  end
498
484
 
499
- end
500
-
501
-
502
-
503
- private
504
-
505
-
506
-
507
- def user_params
508
-
509
- params.require(:user).permit(:name, :email, :department, :password, :password_confirmation)
510
-
511
- end
512
-
513
-
514
-
515
- def basic_info_params
516
-
517
- params.require(:user).permit(:basic_time, :work_time)
518
-
519
- end
520
-
521
-
522
-
523
- def update_index_params
524
-
525
- params.require(:user).permit(:name, :email, :department, :password, :password_confirmation, :basic_time, :work_time, :employee_number, :card_id)
526
-
527
- end
528
-
529
-
530
-
531
- # beforeアクション
532
-
533
-
534
-
535
- # ログイン済みユーザーか確認
536
-
537
- def logged_in_user
538
-
539
- unless logged_in?
540
-
541
- store_location
542
-
543
- flash[:danger] = "ログインしてください。"
544
-
545
- redirect_to login_url
546
-
547
- end
548
-
549
- end
550
-
551
-
552
-
553
- # 正しいユーザーかどうか確認
554
-
555
- def correct_user
556
-
557
- @user = User.find(params[:id])
558
-
559
- redirect_to(root_url) unless current_user?(@user)
560
-
561
- end
562
-
563
-
564
-
565
- # 管理者かどうか確認
566
-
567
- def admin_user
568
-
569
- if !current_user.admin?
570
-
571
- redirect_to(root_url)
572
-
573
- flash[:danger] = "管理者以外アクセスできません。"
574
-
575
- end
576
-
577
- end
578
-
579
-
580
-
581
- #検索機能
582
-
583
- def search_params
584
-
585
- params.require(:q).permit(:name_cont)
586
-
587
- end
588
-
589
-
590
-
591
- #一般ユーザーで他のユーザーのURLを指定しても開けないようにする
592
-
593
- def general_user
594
-
595
- @user = User.find(params[:id])
596
-
597
- if !current_user.admin? && !current_user?(@user)
598
-
599
- redirect_to(root_url)
600
-
601
- end
602
-
603
- end
604
-
605
-
606
-
607
- #管理者は勤怠画面の表示禁止
608
-
609
- def hidden
610
-
611
- if current_user.admin?
612
-
613
- redirect_to(root_url)
614
-
615
- end
616
-
617
- end
618
-
619
-
620
-
621
- def import_emails
622
-
623
- # 登録処理前のレコード数
624
-
625
- current_email_count = ::Email.count
626
-
627
- emails = []
628
-
629
- # windowsで作られたファイルに対応するので、encoding: "SJIS"を付けている
630
-
631
- CSV.foreach(params[:emails_file].path, headers: true, encoding: "SJIS") do |row|
632
-
633
- emails << ::Email.new({ name: row["name"], email: row["email"], department: row["affiliation"], employee_number: row["employee_number"], card_id: row["uid"], basic_time: row["basic_work_time"],
634
-
635
- work_time: row["designated_work_start_time"], work_end_time: row["designated_work_end_time"], admin: row["admin"], password_digest: row["password"] })
636
-
637
- end
638
-
639
- # importメソッドでバルクインサートできる
640
-
641
- ::Email.import(emails)
642
-
643
- # 何レコード登録できたかを返す
644
-
645
- ::Email.count - current_email_count
646
-
647
- end
648
-
649
-
485
+ end
650
486
 
651
487
  end
652
488
 
@@ -682,4 +518,54 @@
682
518
 
683
519
  ```
684
520
 
521
+ ・show.html.erb
522
+
523
+ ```ここに言語を入力
524
+
525
+ <% @dates.each do |day| %>
526
+
527
+ <%= fields_for "attendances[]", day do |af| %>
528
+
529
+ <tr>
530
+
531
+ <td><%= day.worked_on.to_s(:date) %></td>
532
+
533
+ <td class="<%= css_class %>"><%= %w{日 月 火 水 木 金 土}[day.worked_on.wday] %></td>
534
+
535
+ <td><%= day.started_at.to_s(:hour) if day.started_at.present? %></td>
536
+
537
+ <td><%= day.started_at.floor_to(15.minutes).to_s(:min) if day.started_at.present? %></td>
538
+
539
+ <td>
540
+
541
+ <% if day.worked_on == Date.today && day.started_at.nil? %>
542
+
543
+ <%= button_to "出社", user_attendances_path(@user), class: "btn btn-xs btn-primary" %>
544
+
545
+ <% end %>
546
+
547
+ </td>
548
+
549
+ <td><%= day.finished_at.to_s(:hour) if day.finished_at.present? %></td>
550
+
551
+ <td><%= day.finished_at.floor_to(15.minutes).to_s(:min) if day.finished_at.present? %></td>
552
+
553
+ <td>
554
+
555
+ <% if day.worked_on == Date.today && day.started_at.present? && day.finished_at.nil? %>
556
+
557
+ <%= button_to "退社", user_attendances_path(@user), class: "btn btn-xs btn-primary" %>
558
+
559
+ <% end %>
560
+
561
+ </td>
562
+
563
+ </tr>
564
+
565
+ <% end %>
566
+
567
+ <% end %>
568
+
569
+ ```
570
+
685
571
  ![イメージ説明](a79515b419c9efc3d4eb9c6fd133cf46.png)

3

修正

2019/06/10 03:11

投稿

ikt_erk122
ikt_erk122

スコア40

test CHANGED
File without changes
test CHANGED
@@ -176,51 +176,425 @@
176
176
 
177
177
 
178
178
 
179
+ def update
180
+
181
+ @user = User.find(params[:id])
182
+
183
+ if attendances_invalid?
184
+
185
+ attendances_params.each do |id, item|
186
+
187
+ attendance = Attendance.find(id)
188
+
189
+ attendance.update_attributes(item)
190
+
191
+ end
192
+
193
+ flash[:success] = '勤怠情報を更新しました。'
194
+
195
+ redirect_to user_path(@user, params:{first_day: params[:date]})
196
+
197
+ @tomorrow = Time.now.tomorrow ←追加
198
+
199
+ else
200
+
201
+ redirect_to edit_attendances_path(@user, params[:date])
202
+
203
+ end
204
+
205
+ end
206
+
207
+
208
+
209
+ private
210
+
211
+ def attendances_params
212
+
213
+ params.permit(attendances: [:started_at, :finished_at, :note])[:attendances]
214
+
215
+ end
216
+
217
+
218
+
219
+ # ログインしていない一般ユーザーは勤怠編集画面を開けない
220
+
221
+ def general_user
222
+
223
+ @user = User.find(params[:id])
224
+
225
+ if !current_user?(@user) && !current_user.admin?
226
+
227
+ redirect_to(root_url)
228
+
229
+ end
230
+
231
+ end
232
+
233
+
234
+
235
+ # ログイン済みユーザーか確認
236
+
237
+ def logged_in_user
238
+
239
+ unless logged_in?
240
+
241
+ store_location
242
+
243
+ flash[:danger] = "ログインしてください。"
244
+
245
+ redirect_to login_url
246
+
247
+ end
248
+
249
+ end
250
+
251
+
252
+
253
+ #管理者は勤怠編集画面の表示禁止
254
+
255
+ def hidden
256
+
257
+ if current_user.admin?
258
+
259
+ redirect_to(root_url)
260
+
261
+ end
262
+
263
+ end
264
+
265
+ end
266
+
267
+ ```
268
+
269
+ ・attendances_helper
270
+
271
+ ```ここに言語を入力
272
+
273
+
274
+
275
+ def attendances_invalid?
276
+
277
+ attendances = true
278
+
279
+ attendances_params.each do |id, item|
280
+
281
+ if item[:started_at].blank? && item[:finished_at].blank?
282
+
283
+ next
284
+
285
+ elsif item[:started_at].blank? || item[:finished_at].blank?
286
+
287
+ attendances = false
288
+
289
+ flash[:danger] = "出社時間または退社時間を入力してください。"
290
+
291
+ break
292
+
293
+ elsif item[:started_at] > item[:finished_at]
294
+
295
+ attendances = false
296
+
297
+ flash[:danger] = "出社時間を退社時間より遅い時間に設定することはできません。"
298
+
299
+ break
300
+
301
+ end
302
+
303
+ end
304
+
305
+ return attendances
306
+
307
+ end
308
+
309
+ end
310
+
311
+ ```
312
+
313
+ ・users_controller.rb
314
+
315
+ ```ここに言語を入力
316
+
317
+ class UsersController < ApplicationController
318
+
319
+ before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :show]
320
+
321
+ before_action :correct_user, only: [:edit, :update]
322
+
323
+ before_action :admin_user, only: [:destroy, :edit_basic_info, :update_basic_info, :index]
324
+
325
+ before_action :general_user, only: :show
326
+
327
+ before_action :hidden, only: :show
328
+
329
+
330
+
331
+ def new
332
+
333
+ @user = User.new
334
+
335
+ end
336
+
337
+
338
+
339
+ def show
340
+
341
+ @user = User.find(params[:id])
342
+
343
+ if params[:first_day].nil?
344
+
345
+ @first_day = Date.today.beginning_of_month
346
+
347
+ else
348
+
349
+ @first_day = Date.parse(params[:first_day])
350
+
351
+ end
352
+
353
+ @last_day = @first_day.end_of_month
354
+
355
+ (@first_day..@last_day).each do |day|
356
+
357
+ unless @user.attendances.any? {|attendance| attendance.worked_on == day}
358
+
359
+ record = @user.attendances.build(worked_on: day)
360
+
361
+ record.save
362
+
363
+ end
364
+
365
+ end
366
+
367
+ @dates = user_attendances_month_date
368
+
369
+ @worked_sum = @dates.where.not(started_at: nil).count
370
+
371
+ respond_to do |format|
372
+
373
+ format.html
374
+
375
+ format.csv do
376
+
377
+ send_data render_to_string, filename: "#{@user.name}.csv", type: :csv
378
+
379
+ end
380
+
381
+ end
382
+
383
+ end
384
+
385
+
386
+
387
+ def create
388
+
389
+ @user = User.new(user_params)
390
+
391
+ if @user.save
392
+
393
+ log_in @user
394
+
395
+ flash[:success] = "ユーザーの新規作成に成功しました。"
396
+
397
+ redirect_to @user
398
+
399
+ else
400
+
401
+ render 'new'
402
+
403
+ end
404
+
405
+ registered_count = import_emails
406
+
407
+ redirect_to emails_path, notice: "#{registered_count}件登録しました"
408
+
409
+ end
410
+
411
+
412
+
413
+ def edit
414
+
415
+ @user = User.find(params[:id])
416
+
417
+ end
418
+
419
+
420
+
179
421
  def update
180
422
 
181
423
  @user = User.find(params[:id])
182
424
 
183
- if attendances_invalid?
184
-
185
- attendances_params.each do |id, item|
186
-
187
- attendance = Attendance.find(id)
188
-
189
- attendance.update_attributes(item)
425
+ if @user.update_attributes(user_params)
190
-
191
- end
426
+
192
-
193
- flash[:success] = '勤怠情報を更新しました。'
427
+ flash[:success] = "ユーザー情報を更新しました。"
194
-
428
+
195
- redirect_to user_path(@user, params:{first_day: params[:date]})
429
+ redirect_to @user
196
430
 
197
431
  else
198
432
 
199
- redirect_to edit_attendances_path(@user, params[:date])
433
+ render 'edit'
200
-
434
+
201
- end
435
+ end
202
-
436
+
203
- end
437
+ end
438
+
439
+
440
+
204
-
441
+ def index
442
+
205
-
443
+ if params[:q] && params[:q].reject { |key, value| value.blank? }.present?
444
+
445
+ @q = User.ransack(search_params, activated_true: true)
446
+
447
+ @title = "検索結果"
448
+
449
+ else
450
+
451
+ @q = User.ransack(activated_true: true)
452
+
453
+ @title = "ユーザー一覧"
454
+
455
+ end
456
+
457
+ @users = @q.result.paginate(page: params[:page])
458
+
459
+ end
460
+
461
+
462
+
463
+ def destroy
464
+
465
+ User.find(params[:id]).destroy
466
+
467
+ flash[:success] = "削除しました。"
468
+
469
+ redirect_to users_url
470
+
471
+ end
472
+
473
+
474
+
475
+ def edit_index
476
+
477
+ @user = User.find(params[:id])
478
+
479
+ end
480
+
481
+
482
+
483
+ def update_index
484
+
485
+ @user = User.find(params[:id])
486
+
487
+ if @user.update_attributes(update_index_params)
488
+
489
+ flash[:success] = "更新しました。"
490
+
491
+ redirect_to users_url
492
+
493
+ else
494
+
495
+ render 'index'
496
+
497
+ end
498
+
499
+ end
500
+
501
+
206
502
 
207
503
  private
208
504
 
505
+
506
+
209
- def attendances_params
507
+ def user_params
210
-
508
+
211
- params.permit(attendances: [:started_at, :finished_at, :note])[:attendances]
509
+ params.require(:user).permit(:name, :email, :department, :password, :password_confirmation)
212
-
510
+
213
- end
511
+ end
512
+
513
+
514
+
214
-
515
+ def basic_info_params
516
+
215
-
517
+ params.require(:user).permit(:basic_time, :work_time)
518
+
216
-
519
+ end
520
+
521
+
522
+
523
+ def update_index_params
524
+
525
+ params.require(:user).permit(:name, :email, :department, :password, :password_confirmation, :basic_time, :work_time, :employee_number, :card_id)
526
+
527
+ end
528
+
529
+
530
+
531
+ # beforeアクション
532
+
533
+
534
+
217
- # ログインしていない一般ユーザーは勤怠編集画面を開けない
535
+ # ログイン済みユーザーか確認
536
+
537
+ def logged_in_user
538
+
539
+ unless logged_in?
540
+
541
+ store_location
542
+
543
+ flash[:danger] = "ログインしてください。"
544
+
545
+ redirect_to login_url
546
+
547
+ end
548
+
549
+ end
550
+
551
+
552
+
553
+ # 正しいユーザーかどうか確認
554
+
555
+ def correct_user
556
+
557
+ @user = User.find(params[:id])
558
+
559
+ redirect_to(root_url) unless current_user?(@user)
560
+
561
+ end
562
+
563
+
564
+
565
+ # 管理者かどうか確認
566
+
567
+ def admin_user
568
+
569
+ if !current_user.admin?
570
+
571
+ redirect_to(root_url)
572
+
573
+ flash[:danger] = "管理者以外アクセスできません。"
574
+
575
+ end
576
+
577
+ end
578
+
579
+
580
+
581
+ #検索機能
582
+
583
+ def search_params
584
+
585
+ params.require(:q).permit(:name_cont)
586
+
587
+ end
588
+
589
+
590
+
591
+ #一般ユーザーで他のユーザーのURLを指定しても開けないようにする
218
592
 
219
593
  def general_user
220
594
 
221
595
  @user = User.find(params[:id])
222
596
 
223
- if !current_user?(@user) && !current_user.admin?
597
+ if !current_user.admin? && !current_user?(@user)
224
598
 
225
599
  redirect_to(root_url)
226
600
 
@@ -230,25 +604,7 @@
230
604
 
231
605
 
232
606
 
233
- # ログイン済みユーザーか確認
234
-
235
- def logged_in_user
236
-
237
- unless logged_in?
238
-
239
- store_location
240
-
241
- flash[:danger] = "ログインしてください。"
242
-
243
- redirect_to login_url
244
-
245
- end
246
-
247
- end
248
-
249
-
250
-
251
- #管理者は勤怠編集画面の表示禁止
607
+ #管理者は勤怠画面の表示禁止
252
608
 
253
609
  def hidden
254
610
 
@@ -260,380 +616,70 @@
260
616
 
261
617
  end
262
618
 
619
+
620
+
621
+ def import_emails
622
+
623
+ # 登録処理前のレコード数
624
+
625
+ current_email_count = ::Email.count
626
+
627
+ emails = []
628
+
629
+ # windowsで作られたファイルに対応するので、encoding: "SJIS"を付けている
630
+
631
+ CSV.foreach(params[:emails_file].path, headers: true, encoding: "SJIS") do |row|
632
+
633
+ emails << ::Email.new({ name: row["name"], email: row["email"], department: row["affiliation"], employee_number: row["employee_number"], card_id: row["uid"], basic_time: row["basic_work_time"],
634
+
635
+ work_time: row["designated_work_start_time"], work_end_time: row["designated_work_end_time"], admin: row["admin"], password_digest: row["password"] })
636
+
637
+ end
638
+
639
+ # importメソッドでバルクインサートできる
640
+
641
+ ::Email.import(emails)
642
+
643
+ # 何レコード登録できたかを返す
644
+
645
+ ::Email.count - current_email_count
646
+
647
+ end
648
+
649
+
650
+
263
651
  end
264
652
 
653
+
654
+
265
655
  ```
266
656
 
267
- users_controller.rb
657
+ ・schema.rb
268
658
 
269
659
  ```ここに言語を入力
270
660
 
271
- class UsersController < ApplicationController
272
-
273
- before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :show]
274
-
275
- before_action :correct_user, only: [:edit, :update]
276
-
277
- before_action :admin_user, only: [:destroy, :edit_basic_info, :update_basic_info, :index]
278
-
279
- before_action :general_user, only: :show
280
-
281
- before_action :hidden, only: :show
282
-
283
-
284
-
285
- def new
286
-
287
- @user = User.new
288
-
289
- end
290
-
291
-
292
-
293
- def show
294
-
295
- @user = User.find(params[:id])
296
-
297
- if params[:first_day].nil?
298
-
299
- @first_day = Date.today.beginning_of_month
300
-
301
- else
302
-
303
- @first_day = Date.parse(params[:first_day])
304
-
305
- end
306
-
307
- @last_day = @first_day.end_of_month
308
-
309
- (@first_day..@last_day).each do |day|
310
-
311
- unless @user.attendances.any? {|attendance| attendance.worked_on == day}
312
-
313
- record = @user.attendances.build(worked_on: day)
314
-
315
- record.save
316
-
317
- end
318
-
319
- end
320
-
321
- @dates = user_attendances_month_date
322
-
323
- @worked_sum = @dates.where.not(started_at: nil).count
324
-
325
- respond_to do |format|
326
-
327
- format.html
328
-
329
- format.csv do
330
-
331
- send_data render_to_string, filename: "#{@user.name}.csv", type: :csv
332
-
333
- end
334
-
335
- end
336
-
337
- end
338
-
339
-
340
-
341
- def create
342
-
343
- @user = User.new(user_params)
344
-
345
- if @user.save
346
-
347
- log_in @user
348
-
349
- flash[:success] = "ユーザーの新規作成に成功しました。"
350
-
351
- redirect_to @user
352
-
353
- else
354
-
355
- render 'new'
356
-
357
- end
358
-
359
- registered_count = import_emails
360
-
361
- redirect_to emails_path, notice: "#{registered_count}件登録しました"
362
-
363
- end
364
-
365
-
366
-
367
- def edit
368
-
369
- @user = User.find(params[:id])
370
-
371
- end
372
-
373
-
374
-
375
- def update
376
-
377
- @user = User.find(params[:id])
378
-
379
- if @user.update_attributes(user_params)
380
-
381
- flash[:success] = "ユーザー情報を更新しました。"
382
-
383
- redirect_to @user
384
-
385
- else
386
-
387
- render 'edit'
388
-
389
- end
390
-
391
- end
392
-
393
-
394
-
395
- def index
396
-
397
- if params[:q] && params[:q].reject { |key, value| value.blank? }.present?
398
-
399
- @q = User.ransack(search_params, activated_true: true)
400
-
401
- @title = "検索結果"
402
-
403
- else
404
-
405
- @q = User.ransack(activated_true: true)
406
-
407
- @title = "ユーザー一覧"
408
-
409
- end
410
-
411
- @users = @q.result.paginate(page: params[:page])
412
-
413
- end
414
-
415
-
416
-
417
- def destroy
418
-
419
- User.find(params[:id]).destroy
420
-
421
- flash[:success] = "削除しました。"
422
-
423
- redirect_to users_url
424
-
425
- end
426
-
427
-
428
-
429
- def edit_index
430
-
431
- @user = User.find(params[:id])
432
-
433
- end
434
-
435
-
436
-
437
- def update_index
438
-
439
- @user = User.find(params[:id])
440
-
441
- if @user.update_attributes(update_index_params)
442
-
443
- flash[:success] = "更新しました。"
444
-
445
- redirect_to users_url
446
-
447
- else
448
-
449
- render 'index'
450
-
451
- end
452
-
453
- end
454
-
455
-
456
-
457
- private
458
-
459
-
460
-
461
- def user_params
462
-
463
- params.require(:user).permit(:name, :email, :department, :password, :password_confirmation)
464
-
465
- end
466
-
467
-
468
-
469
- def basic_info_params
470
-
471
- params.require(:user).permit(:basic_time, :work_time)
472
-
473
- end
474
-
475
-
476
-
477
- def update_index_params
478
-
479
- params.require(:user).permit(:name, :email, :department, :password, :password_confirmation, :basic_time, :work_time, :employee_number, :card_id)
480
-
481
- end
482
-
483
-
484
-
485
- # beforeアクション
486
-
487
-
488
-
489
- # ログイン済みユーザーか確認
490
-
491
- def logged_in_user
492
-
493
- unless logged_in?
494
-
495
- store_location
496
-
497
- flash[:danger] = "ログインしてください。"
498
-
499
- redirect_to login_url
500
-
501
- end
502
-
503
- end
504
-
505
-
506
-
507
- # 正しいユーザーかどうか確認
508
-
509
- def correct_user
510
-
511
- @user = User.find(params[:id])
512
-
513
- redirect_to(root_url) unless current_user?(@user)
514
-
515
- end
516
-
517
-
518
-
519
- # 管理者かどうか確認
520
-
521
- def admin_user
522
-
523
- if !current_user.admin?
524
-
525
- redirect_to(root_url)
526
-
527
- flash[:danger] = "管理者以外アクセスできません。"
528
-
529
- end
530
-
531
- end
532
-
533
-
534
-
535
- #検索機能
536
-
537
- def search_params
538
-
539
- params.require(:q).permit(:name_cont)
540
-
541
- end
542
-
543
-
544
-
545
- #一般ユーザーで他のユーザーのURLを指定しても開けないようにする
546
-
547
- def general_user
548
-
549
- @user = User.find(params[:id])
550
-
551
- if !current_user.admin? && !current_user?(@user)
552
-
553
- redirect_to(root_url)
554
-
555
- end
556
-
557
- end
558
-
559
-
560
-
561
- #管理者は勤怠画面の表示禁止
562
-
563
- def hidden
564
-
565
- if current_user.admin?
566
-
567
- redirect_to(root_url)
568
-
569
- end
570
-
571
- end
572
-
573
-
574
-
575
- def import_emails
576
-
577
- # 登録処理前のレコード数
578
-
579
- current_email_count = ::Email.count
580
-
581
- emails = []
582
-
583
- # windowsで作られたファイルに対応するので、encoding: "SJIS"を付けている
584
-
585
- CSV.foreach(params[:emails_file].path, headers: true, encoding: "SJIS") do |row|
586
-
587
- emails << ::Email.new({ name: row["name"], email: row["email"], department: row["affiliation"], employee_number: row["employee_number"], card_id: row["uid"], basic_time: row["basic_work_time"],
588
-
589
- work_time: row["designated_work_start_time"], work_end_time: row["designated_work_end_time"], admin: row["admin"], password_digest: row["password"] })
590
-
591
- end
592
-
593
- # importメソッドでバルクインサートできる
594
-
595
- ::Email.import(emails)
596
-
597
- # 何レコード登録できたかを返す
598
-
599
- ::Email.count - current_email_count
600
-
601
- end
602
-
603
-
604
-
605
- end
606
-
607
-
661
+ create_table "attendances", force: :cascade do |t|
662
+
663
+ t.date "worked_on"
664
+
665
+ t.datetime "started_at"
666
+
667
+ t.datetime "finished_at"
668
+
669
+ t.string "note"
670
+
671
+ t.integer "user_id"
672
+
673
+ t.datetime "created_at", null: false
674
+
675
+ t.datetime "updated_at", null: false
676
+
677
+ t.datetime "next_day"
678
+
679
+ t.index ["user_id"], name: "index_attendances_on_user_id"
680
+
681
+ end
608
682
 
609
683
  ```
610
684
 
611
- ・schema.rb
612
-
613
- ```ここに言語を入力
614
-
615
- create_table "attendances", force: :cascade do |t|
616
-
617
- t.date "worked_on"
618
-
619
- t.datetime "started_at"
620
-
621
- t.datetime "finished_at"
622
-
623
- t.string "note"
624
-
625
- t.integer "user_id"
626
-
627
- t.datetime "created_at", null: false
628
-
629
- t.datetime "updated_at", null: false
630
-
631
- t.datetime "next_day"
632
-
633
- t.index ["user_id"], name: "index_attendances_on_user_id"
634
-
635
- end
636
-
637
- ```
638
-
639
685
  ![イメージ説明](a79515b419c9efc3d4eb9c6fd133cf46.png)

2

修正

2019/06/09 07:11

投稿

ikt_erk122
ikt_erk122

スコア40

test CHANGED
File without changes
test CHANGED
@@ -122,7 +122,47 @@
122
122
 
123
123
  ```ここに言語を入力
124
124
 
125
+ class AttendancesController < ApplicationController
126
+
127
+ before_action :logged_in_user, only: :edit
128
+
129
+ before_action :general_user, only: :edit
130
+
131
+ before_action :hidden, only: :edit
132
+
133
+
134
+
135
+ def create
136
+
137
+ @user = User.find(params[:user_id])
138
+
139
+ @attendance = @user.attendances.find_by(worked_on: Date.today)
140
+
141
+ if @attendance.started_at.nil?
142
+
143
+ @attendance.update_attributes(started_at: current_time)
144
+
145
+ flash[:info] = 'おはようございます。'
146
+
147
+ elsif @attendance.finished_at.nil?
148
+
149
+ @attendance.update_attributes(finished_at: current_time)
150
+
151
+ flash[:info] = 'おつかれさまでした。'
152
+
153
+ else
154
+
155
+ flash[:danger] = 'トラブルがあり、登録出来ませんでした。'
156
+
157
+ end
158
+
159
+ redirect_to @user
160
+
161
+ end
162
+
163
+
164
+
125
- def edit
165
+ def edit
126
166
 
127
167
  @user = User.find(params[:id])
128
168
 
@@ -134,8 +174,440 @@
134
174
 
135
175
  end
136
176
 
177
+
178
+
179
+ def update
180
+
181
+ @user = User.find(params[:id])
182
+
183
+ if attendances_invalid?
184
+
185
+ attendances_params.each do |id, item|
186
+
187
+ attendance = Attendance.find(id)
188
+
189
+ attendance.update_attributes(item)
190
+
191
+ end
192
+
193
+ flash[:success] = '勤怠情報を更新しました。'
194
+
195
+ redirect_to user_path(@user, params:{first_day: params[:date]})
196
+
197
+ else
198
+
199
+ redirect_to edit_attendances_path(@user, params[:date])
200
+
201
+ end
202
+
203
+ end
204
+
205
+
206
+
207
+ private
208
+
209
+ def attendances_params
210
+
211
+ params.permit(attendances: [:started_at, :finished_at, :note])[:attendances]
212
+
213
+ end
214
+
215
+
216
+
217
+ # ログインしていない一般ユーザーは勤怠編集画面を開けない
218
+
219
+ def general_user
220
+
221
+ @user = User.find(params[:id])
222
+
223
+ if !current_user?(@user) && !current_user.admin?
224
+
225
+ redirect_to(root_url)
226
+
227
+ end
228
+
229
+ end
230
+
231
+
232
+
233
+ # ログイン済みユーザーか確認
234
+
235
+ def logged_in_user
236
+
237
+ unless logged_in?
238
+
239
+ store_location
240
+
241
+ flash[:danger] = "ログインしてください。"
242
+
243
+ redirect_to login_url
244
+
245
+ end
246
+
247
+ end
248
+
249
+
250
+
251
+ #管理者は勤怠編集画面の表示禁止
252
+
253
+ def hidden
254
+
255
+ if current_user.admin?
256
+
257
+ redirect_to(root_url)
258
+
259
+ end
260
+
261
+ end
262
+
263
+ end
264
+
137
265
  ```
138
266
 
267
+ ・users_controller.rb
268
+
269
+ ```ここに言語を入力
270
+
271
+ class UsersController < ApplicationController
272
+
273
+ before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :show]
274
+
275
+ before_action :correct_user, only: [:edit, :update]
276
+
277
+ before_action :admin_user, only: [:destroy, :edit_basic_info, :update_basic_info, :index]
278
+
279
+ before_action :general_user, only: :show
280
+
281
+ before_action :hidden, only: :show
282
+
283
+
284
+
285
+ def new
286
+
287
+ @user = User.new
288
+
289
+ end
290
+
291
+
292
+
293
+ def show
294
+
295
+ @user = User.find(params[:id])
296
+
297
+ if params[:first_day].nil?
298
+
299
+ @first_day = Date.today.beginning_of_month
300
+
301
+ else
302
+
303
+ @first_day = Date.parse(params[:first_day])
304
+
305
+ end
306
+
307
+ @last_day = @first_day.end_of_month
308
+
309
+ (@first_day..@last_day).each do |day|
310
+
311
+ unless @user.attendances.any? {|attendance| attendance.worked_on == day}
312
+
313
+ record = @user.attendances.build(worked_on: day)
314
+
315
+ record.save
316
+
317
+ end
318
+
319
+ end
320
+
321
+ @dates = user_attendances_month_date
322
+
323
+ @worked_sum = @dates.where.not(started_at: nil).count
324
+
325
+ respond_to do |format|
326
+
327
+ format.html
328
+
329
+ format.csv do
330
+
331
+ send_data render_to_string, filename: "#{@user.name}.csv", type: :csv
332
+
333
+ end
334
+
335
+ end
336
+
337
+ end
338
+
339
+
340
+
341
+ def create
342
+
343
+ @user = User.new(user_params)
344
+
345
+ if @user.save
346
+
347
+ log_in @user
348
+
349
+ flash[:success] = "ユーザーの新規作成に成功しました。"
350
+
351
+ redirect_to @user
352
+
353
+ else
354
+
355
+ render 'new'
356
+
357
+ end
358
+
359
+ registered_count = import_emails
360
+
361
+ redirect_to emails_path, notice: "#{registered_count}件登録しました"
362
+
363
+ end
364
+
365
+
366
+
367
+ def edit
368
+
369
+ @user = User.find(params[:id])
370
+
371
+ end
372
+
373
+
374
+
375
+ def update
376
+
377
+ @user = User.find(params[:id])
378
+
379
+ if @user.update_attributes(user_params)
380
+
381
+ flash[:success] = "ユーザー情報を更新しました。"
382
+
383
+ redirect_to @user
384
+
385
+ else
386
+
387
+ render 'edit'
388
+
389
+ end
390
+
391
+ end
392
+
393
+
394
+
395
+ def index
396
+
397
+ if params[:q] && params[:q].reject { |key, value| value.blank? }.present?
398
+
399
+ @q = User.ransack(search_params, activated_true: true)
400
+
401
+ @title = "検索結果"
402
+
403
+ else
404
+
405
+ @q = User.ransack(activated_true: true)
406
+
407
+ @title = "ユーザー一覧"
408
+
409
+ end
410
+
411
+ @users = @q.result.paginate(page: params[:page])
412
+
413
+ end
414
+
415
+
416
+
417
+ def destroy
418
+
419
+ User.find(params[:id]).destroy
420
+
421
+ flash[:success] = "削除しました。"
422
+
423
+ redirect_to users_url
424
+
425
+ end
426
+
427
+
428
+
429
+ def edit_index
430
+
431
+ @user = User.find(params[:id])
432
+
433
+ end
434
+
435
+
436
+
437
+ def update_index
438
+
439
+ @user = User.find(params[:id])
440
+
441
+ if @user.update_attributes(update_index_params)
442
+
443
+ flash[:success] = "更新しました。"
444
+
445
+ redirect_to users_url
446
+
447
+ else
448
+
449
+ render 'index'
450
+
451
+ end
452
+
453
+ end
454
+
455
+
456
+
457
+ private
458
+
459
+
460
+
461
+ def user_params
462
+
463
+ params.require(:user).permit(:name, :email, :department, :password, :password_confirmation)
464
+
465
+ end
466
+
467
+
468
+
469
+ def basic_info_params
470
+
471
+ params.require(:user).permit(:basic_time, :work_time)
472
+
473
+ end
474
+
475
+
476
+
477
+ def update_index_params
478
+
479
+ params.require(:user).permit(:name, :email, :department, :password, :password_confirmation, :basic_time, :work_time, :employee_number, :card_id)
480
+
481
+ end
482
+
483
+
484
+
485
+ # beforeアクション
486
+
487
+
488
+
489
+ # ログイン済みユーザーか確認
490
+
491
+ def logged_in_user
492
+
493
+ unless logged_in?
494
+
495
+ store_location
496
+
497
+ flash[:danger] = "ログインしてください。"
498
+
499
+ redirect_to login_url
500
+
501
+ end
502
+
503
+ end
504
+
505
+
506
+
507
+ # 正しいユーザーかどうか確認
508
+
509
+ def correct_user
510
+
511
+ @user = User.find(params[:id])
512
+
513
+ redirect_to(root_url) unless current_user?(@user)
514
+
515
+ end
516
+
517
+
518
+
519
+ # 管理者かどうか確認
520
+
521
+ def admin_user
522
+
523
+ if !current_user.admin?
524
+
525
+ redirect_to(root_url)
526
+
527
+ flash[:danger] = "管理者以外アクセスできません。"
528
+
529
+ end
530
+
531
+ end
532
+
533
+
534
+
535
+ #検索機能
536
+
537
+ def search_params
538
+
539
+ params.require(:q).permit(:name_cont)
540
+
541
+ end
542
+
543
+
544
+
545
+ #一般ユーザーで他のユーザーのURLを指定しても開けないようにする
546
+
547
+ def general_user
548
+
549
+ @user = User.find(params[:id])
550
+
551
+ if !current_user.admin? && !current_user?(@user)
552
+
553
+ redirect_to(root_url)
554
+
555
+ end
556
+
557
+ end
558
+
559
+
560
+
561
+ #管理者は勤怠画面の表示禁止
562
+
563
+ def hidden
564
+
565
+ if current_user.admin?
566
+
567
+ redirect_to(root_url)
568
+
569
+ end
570
+
571
+ end
572
+
573
+
574
+
575
+ def import_emails
576
+
577
+ # 登録処理前のレコード数
578
+
579
+ current_email_count = ::Email.count
580
+
581
+ emails = []
582
+
583
+ # windowsで作られたファイルに対応するので、encoding: "SJIS"を付けている
584
+
585
+ CSV.foreach(params[:emails_file].path, headers: true, encoding: "SJIS") do |row|
586
+
587
+ emails << ::Email.new({ name: row["name"], email: row["email"], department: row["affiliation"], employee_number: row["employee_number"], card_id: row["uid"], basic_time: row["basic_work_time"],
588
+
589
+ work_time: row["designated_work_start_time"], work_end_time: row["designated_work_end_time"], admin: row["admin"], password_digest: row["password"] })
590
+
591
+ end
592
+
593
+ # importメソッドでバルクインサートできる
594
+
595
+ ::Email.import(emails)
596
+
597
+ # 何レコード登録できたかを返す
598
+
599
+ ::Email.count - current_email_count
600
+
601
+ end
602
+
603
+
604
+
605
+ end
606
+
607
+
608
+
609
+ ```
610
+
139
611
  ・schema.rb
140
612
 
141
613
  ```ここに言語を入力

1

修正

2019/06/08 13:54

投稿

ikt_erk122
ikt_erk122

スコア40

test CHANGED
@@ -1 +1 @@
1
- チェックボックスを押されているときに翌日の時間を足すようにしたい
1
+ チェックボックスを押されているときに翌日の時間を足すようにしたい
test CHANGED
@@ -1,12 +1,14 @@
1
1
  【やりたいこと】
2
2
 
3
- チェックボックスを押しきに翌日の時間を足す(+24時間)したい
3
+ 出社・退社時間を編集する機能を追加する勉強をしています。編集する際にチェックボックスを押して編集すると翌日に退社(24時間)とい機能を追加したいです。
4
4
 
5
5
 
6
6
 
7
7
  【困っていること】
8
8
 
9
9
  チェックボックスは実装できているが、翌日の時間を取得する方法や書き方を調べてもわからず押しても何も変わらない状態。
10
+
11
+ 自分の考えはDateクラスのDate.tomorrowを使えばいけるのかなと思っています。
10
12
 
11
13
 
12
14
 
@@ -20,69 +22,97 @@
20
22
 
21
23
  ```ここに言語を入力
22
24
 
23
- <% @dates.each do |day| %>
25
+ <%= form_for(@user, url: update_attendances_path, method: :patch) do |f| %>
24
26
 
25
- <%= fields_for "attendances[]", day do |af| %>
27
+ <table class = "table-bordered table-striped table-condensed">
26
28
 
27
- <tr>
29
+ <thead>
28
30
 
29
- <td><%= day.worked_on.to_s(:date) %></td>
31
+ <tr>
30
32
 
31
- <td><%= %w{日 水 木 金 土}[day.worked_on.wday] %></td>
33
+ <th>日付</th>
32
34
 
33
- <td>
35
+ <th>曜日</th>
34
36
 
35
- <% if day.worked_on > Date.today %>
37
+ <th>出社時間</th>
36
38
 
37
- <%= af.time_field :started_at, :readonly => true, class: "form-control" %>
39
+ <th>退社時間</th>
38
40
 
39
- <% else %>
41
+ <th>翌日</th>
40
42
 
41
- <%= af.time_field :started_at, class: "form-control" %>
43
+ </tr>
42
44
 
43
- <% end %>
45
+ </thead>
44
46
 
45
- </td>
47
+ <tbody>
46
48
 
47
- <td>
49
+ <% @dates.each do |day| %>
48
50
 
49
- <% if day.worked_on > Date.today %>
51
+ <%= fields_for "attendances[]", day do |af| %>
50
52
 
51
- <%= af.time_field :finished_at, :readonly => true, class: "form-control" %>
53
+ <tr>
52
54
 
53
- <% else %>
55
+ <td><%= day.worked_on.to_s(:date) %></td>
54
56
 
55
- <%= af.time_field :finished_at, class: "form-control" %>
57
+ <td><%= %w{日 月 火 水 木 金 土}[day.worked_on.wday] %></td>
56
58
 
57
- <% end %>
59
+ <td>
58
60
 
59
- </td>
61
+ <% if day.worked_on > Date.today %>
60
62
 
61
- <td>
63
+ <%= af.time_field :started_at, :readonly => true, class: "form-control" %>
62
64
 
63
- <% if day.worked_on > Date.today %>
65
+ <% else %>
64
66
 
65
- <% else %>
67
+ <%= af.time_field :started_at, class: "form-control" %>
66
68
 
69
+ <% end %>
70
+
71
+ </td>
72
+
73
+ <td>
74
+
75
+ <% if day.worked_on > Date.today %>
76
+
77
+ <%= af.time_field :finished_at, :readonly => true, class: "form-control" %>
78
+
79
+ <% else %>
80
+
81
+ <%= af.time_field :finished_at, class: "form-control" %>
82
+
83
+ <% end %>
84
+
85
+ </td>
86
+
87
+ <td>
88
+
89
+ <% if day.worked_on > Date.today %>
90
+
91
+ <% else %>
92
+
67
- <%= af.check_box :next_day, {}, "true", "false" %>
93
+ <%= af.check_box :next_day, {}, "true", "false" %>
94
+
95
+ <% end %>
96
+
97
+ </td>
98
+
99
+ </tr>
100
+
101
+ <% end %>
68
102
 
69
103
  <% end %>
70
104
 
71
- </td>
105
+ </tbody>
72
106
 
73
- <td>
107
+ </table>
74
108
 
75
- <% if day.started_at.present? && day.finished_at.present? %>
109
+ <div class="btn-attendances-update">
76
110
 
77
- <%= working_times(day.started_at, day.finished_at) %>
111
+ <%= f.submit "更新", class: "btn btn-primary" %>
78
112
 
79
- <% end %>
113
+ <%= link_to "キャンセル", user_path(@user, params:{first_day: @first_day }), class: "btn btn-default btn-block" %>
80
114
 
81
- </td>
115
+ </div>
82
-
83
- <td><%= af.text_field :note, class: "form-control" %></td>
84
-
85
- </tr>
86
116
 
87
117
  <% end %>
88
118
 
@@ -106,4 +136,32 @@
106
136
 
107
137
  ```
108
138
 
139
+ ・schema.rb
140
+
141
+ ```ここに言語を入力
142
+
143
+ create_table "attendances", force: :cascade do |t|
144
+
145
+ t.date "worked_on"
146
+
147
+ t.datetime "started_at"
148
+
149
+ t.datetime "finished_at"
150
+
151
+ t.string "note"
152
+
153
+ t.integer "user_id"
154
+
155
+ t.datetime "created_at", null: false
156
+
157
+ t.datetime "updated_at", null: false
158
+
159
+ t.datetime "next_day"
160
+
161
+ t.index ["user_id"], name: "index_attendances_on_user_id"
162
+
163
+ end
164
+
165
+ ```
166
+
109
- ![![イメージ説明](37c937b30c5fb0f826772af164928aca.png)]
167
+ ![イメージ説明](a79515b419c9efc3d4eb9c6fd133cf46.png)