teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

8

修正

2019/06/25 05:55

投稿

ikt_erk122
ikt_erk122

スコア40

title CHANGED
File without changes
body CHANGED
@@ -256,7 +256,7 @@
256
256
  t.integer "user_id"
257
257
  t.datetime "created_at", null: false
258
258
  t.datetime "updated_at", null: false
259
- t.datetime "next_day"
259
+ t.boolean "next_day", default: false
260
260
  t.index ["user_id"], name: "index_attendances_on_user_id"
261
261
  end
262
262
  ```

7

修正

2019/06/25 05:55

投稿

ikt_erk122
ikt_erk122

スコア40

title CHANGED
File without changes
body CHANGED
@@ -262,6 +262,7 @@
262
262
  ```
263
263
  ・show.html.erb
264
264
  ```ここに言語を入力
265
+ <tbody>
265
266
  <% @dates.each do |day| %>
266
267
  <%= fields_for "attendances[]", day do |af| %>
267
268
  <tr>
@@ -281,9 +282,17 @@
281
282
  <%= button_to "退社", user_attendances_path(@user), class: "btn btn-xs btn-primary" %>
282
283
  <% end %>
283
284
  </td>
285
+   <td>
286
+  <% if day.started_at.present? && day.finished_at.present? %>
287
+ <%= working_times(day.started_at, day.finished_at) %>
288
+ <% seconds = (day.finished_at - day.started_at).to_i %>
289
+ <% @total_seconds = @total_seconds.to_i + seconds %>
290
+ <% end %>
291
+ </td>
284
292
  </tr>
285
293
  <% end %>
286
294
  <% end %>
295
+ </tbody>
287
296
  ```
288
297
  ・見本(編集前)
289
298
  ![イメージ説明](aa6d4b40ec3ef29993ecea68f4ac53bd.png)

6

修正

2019/06/25 02:58

投稿

ikt_erk122
ikt_erk122

スコア40

title CHANGED
File without changes
body CHANGED
@@ -93,7 +93,9 @@
93
93
  end
94
94
  flash[:success] = '勤怠情報を更新しました。'
95
95
  redirect_to user_path(@user, params:{first_day: params[:date]})
96
+ if params[:next_day] == true
96
- @tomorrow = Time.now.tomorrow ←追加
97
+ Time.now.tomorrow
98
+ end
97
99
  else
98
100
  redirect_to edit_attendances_path(@user, params[:date])
99
101
  end

5

修正

2019/06/24 06:09

投稿

ikt_erk122
ikt_erk122

スコア40

title CHANGED
File without changes
body CHANGED
@@ -283,4 +283,7 @@
283
283
  <% end %>
284
284
  <% end %>
285
285
  ```
286
+ ・見本(編集前)
286
- ![イメージ説明](a79515b419c9efc3d4eb9c6fd133cf46.png)
287
+ ![イメージ説明](aa6d4b40ec3ef29993ecea68f4ac53bd.png)
288
+ ・見本(編集後)
289
+ ![イメージ説明](99ef552b3582f8e6bc1c02086eacf3c6.png)

4

修正

2019/06/24 03:06

投稿

ikt_erk122
ikt_erk122

スコア40

title CHANGED
File without changes
body CHANGED
@@ -5,9 +5,6 @@
5
5
  チェックボックスは実装できているが、翌日の時間を取得する方法や書き方を調べてもわからず押しても何も変わらない状態。
6
6
  自分の考えはDateクラスのDate.tomorrowを使えばいけるのかなと思っています。
7
7
 
8
- 【質問】
9
- 丸投げになって申し訳ないですがアドバイスいただけると幸いです。よろしくお願いします。
10
-
11
8
  ・attendances/edit.html.erb
12
9
  ```ここに言語を入力
13
10
  <%= form_for(@user, url: update_attendances_path, method: :patch) do |f| %>
@@ -157,11 +154,7 @@
157
154
  ・users_controller.rb
158
155
  ```ここに言語を入力
159
156
  class UsersController < ApplicationController
160
- before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :show]
157
+
161
- before_action :correct_user, only: [:edit, :update]
162
- before_action :admin_user, only: [:destroy, :edit_basic_info, :update_basic_info, :index]
163
- before_action :general_user, only: :show
164
- before_action :hidden, only: :show
165
158
 
166
159
  def new
167
160
  @user = User.new
@@ -247,82 +240,7 @@
247
240
  else
248
241
  render 'index'
249
242
  end
250
- end
251
-
252
- private
253
-
254
- def user_params
255
- params.require(:user).permit(:name, :email, :department, :password, :password_confirmation)
256
- end
257
-
258
- def basic_info_params
259
- params.require(:user).permit(:basic_time, :work_time)
260
- end
261
-
262
- def update_index_params
263
- params.require(:user).permit(:name, :email, :department, :password, :password_confirmation, :basic_time, :work_time, :employee_number, :card_id)
264
- end
265
-
266
- # beforeアクション
267
-
268
- # ログイン済みユーザーか確認
269
- def logged_in_user
270
- unless logged_in?
271
- store_location
272
- flash[:danger] = "ログインしてください。"
273
- redirect_to login_url
274
- end
275
- end
276
-
277
- # 正しいユーザーかどうか確認
278
- def correct_user
279
- @user = User.find(params[:id])
280
- redirect_to(root_url) unless current_user?(@user)
281
- end
282
-
283
- # 管理者かどうか確認
284
- def admin_user
285
- if !current_user.admin?
286
- redirect_to(root_url)
287
- flash[:danger] = "管理者以外アクセスできません。"
288
- end
289
- end
290
-
291
- #検索機能
292
- def search_params
293
- params.require(:q).permit(:name_cont)
294
- end
295
-
296
- #一般ユーザーで他のユーザーのURLを指定しても開けないようにする
297
- def general_user
298
- @user = User.find(params[:id])
299
- if !current_user.admin? && !current_user?(@user)
300
- redirect_to(root_url)
301
- end
302
- end
303
-
304
- #管理者は勤怠画面の表示禁止
305
- def hidden
306
- if current_user.admin?
307
- redirect_to(root_url)
308
- end
309
- end
310
-
311
- def import_emails
312
- # 登録処理前のレコード数
313
- current_email_count = ::Email.count
314
- emails = []
315
- # windowsで作られたファイルに対応するので、encoding: "SJIS"を付けている
316
- CSV.foreach(params[:emails_file].path, headers: true, encoding: "SJIS") do |row|
317
- 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"],
318
- work_time: row["designated_work_start_time"], work_end_time: row["designated_work_end_time"], admin: row["admin"], password_digest: row["password"] })
319
- end
320
- # importメソッドでバルクインサートできる
321
- ::Email.import(emails)
322
- # 何レコード登録できたかを返す
323
- ::Email.count - current_email_count
324
- end
325
-
243
+ end
326
244
  end
327
245
 
328
246
  ```
@@ -340,4 +258,29 @@
340
258
  t.index ["user_id"], name: "index_attendances_on_user_id"
341
259
  end
342
260
  ```
261
+ ・show.html.erb
262
+ ```ここに言語を入力
263
+ <% @dates.each do |day| %>
264
+ <%= fields_for "attendances[]", day do |af| %>
265
+ <tr>
266
+ <td><%= day.worked_on.to_s(:date) %></td>
267
+ <td class="<%= css_class %>"><%= %w{日 月 火 水 木 金 土}[day.worked_on.wday] %></td>
268
+ <td><%= day.started_at.to_s(:hour) if day.started_at.present? %></td>
269
+ <td><%= day.started_at.floor_to(15.minutes).to_s(:min) if day.started_at.present? %></td>
270
+ <td>
271
+ <% if day.worked_on == Date.today && day.started_at.nil? %>
272
+ <%= button_to "出社", user_attendances_path(@user), class: "btn btn-xs btn-primary" %>
273
+ <% end %>
274
+ </td>
275
+ <td><%= day.finished_at.to_s(:hour) if day.finished_at.present? %></td>
276
+ <td><%= day.finished_at.floor_to(15.minutes).to_s(:min) if day.finished_at.present? %></td>
277
+ <td>
278
+ <% if day.worked_on == Date.today && day.started_at.present? && day.finished_at.nil? %>
279
+ <%= button_to "退社", user_attendances_path(@user), class: "btn btn-xs btn-primary" %>
280
+ <% end %>
281
+ </td>
282
+ </tr>
283
+ <% end %>
284
+ <% end %>
285
+ ```
343
286
  ![イメージ説明](a79515b419c9efc3d4eb9c6fd133cf46.png)

3

修正

2019/06/10 03:11

投稿

ikt_erk122
ikt_erk122

スコア40

title CHANGED
File without changes
body CHANGED
@@ -87,7 +87,7 @@
87
87
  @dates = user_attendances_month_date
88
88
  end
89
89
 
90
- def update
90
+ def update
91
91
  @user = User.find(params[:id])
92
92
  if attendances_invalid?
93
93
  attendances_params.each do |id, item|
@@ -96,6 +96,7 @@
96
96
  end
97
97
  flash[:success] = '勤怠情報を更新しました。'
98
98
  redirect_to user_path(@user, params:{first_day: params[:date]})
99
+ @tomorrow = Time.now.tomorrow ←追加
99
100
  else
100
101
  redirect_to edit_attendances_path(@user, params[:date])
101
102
  end
@@ -131,6 +132,28 @@
131
132
  end
132
133
  end
133
134
  ```
135
+ ・attendances_helper
136
+ ```ここに言語を入力
137
+
138
+ def attendances_invalid?
139
+ attendances = true
140
+ attendances_params.each do |id, item|
141
+ if item[:started_at].blank? && item[:finished_at].blank?
142
+ next
143
+ elsif item[:started_at].blank? || item[:finished_at].blank?
144
+ attendances = false
145
+ flash[:danger] = "出社時間または退社時間を入力してください。"
146
+ break
147
+ elsif item[:started_at] > item[:finished_at]
148
+ attendances = false
149
+ flash[:danger] = "出社時間を退社時間より遅い時間に設定することはできません。"
150
+ break
151
+ end
152
+ end
153
+ return attendances
154
+ end
155
+ end
156
+ ```
134
157
  ・users_controller.rb
135
158
  ```ここに言語を入力
136
159
  class UsersController < ApplicationController

2

修正

2019/06/09 07:11

投稿

ikt_erk122
ikt_erk122

スコア40

title CHANGED
File without changes
body CHANGED
@@ -60,13 +60,249 @@
60
60
  ```
61
61
  ・attedances_controller.rb
62
62
  ```ここに言語を入力
63
+ class AttendancesController < ApplicationController
64
+ before_action :logged_in_user, only: :edit
65
+ before_action :general_user, only: :edit
66
+ before_action :hidden, only: :edit
67
+
68
+ def create
69
+ @user = User.find(params[:user_id])
70
+ @attendance = @user.attendances.find_by(worked_on: Date.today)
71
+ if @attendance.started_at.nil?
72
+ @attendance.update_attributes(started_at: current_time)
73
+ flash[:info] = 'おはようございます。'
74
+ elsif @attendance.finished_at.nil?
75
+ @attendance.update_attributes(finished_at: current_time)
76
+ flash[:info] = 'おつかれさまでした。'
77
+ else
78
+ flash[:danger] = 'トラブルがあり、登録出来ませんでした。'
79
+ end
80
+ redirect_to @user
81
+ end
82
+
63
- def edit
83
+ def edit
64
84
  @user = User.find(params[:id])
65
85
  @first_day = first_day(params[:date])
66
86
  @last_day = @first_day.end_of_month
67
87
  @dates = user_attendances_month_date
68
88
  end
89
+
90
+ def update
91
+ @user = User.find(params[:id])
92
+ if attendances_invalid?
93
+ attendances_params.each do |id, item|
94
+ attendance = Attendance.find(id)
95
+ attendance.update_attributes(item)
96
+ end
97
+ flash[:success] = '勤怠情報を更新しました。'
98
+ redirect_to user_path(@user, params:{first_day: params[:date]})
99
+ else
100
+ redirect_to edit_attendances_path(@user, params[:date])
101
+ end
102
+ end
103
+
104
+ private
105
+ def attendances_params
106
+ params.permit(attendances: [:started_at, :finished_at, :note])[:attendances]
107
+ end
108
+
109
+ # ログインしていない一般ユーザーは勤怠編集画面を開けない
110
+ def general_user
111
+ @user = User.find(params[:id])
112
+ if !current_user?(@user) && !current_user.admin?
113
+ redirect_to(root_url)
114
+ end
115
+ end
116
+
117
+ # ログイン済みユーザーか確認
118
+ def logged_in_user
119
+ unless logged_in?
120
+ store_location
121
+ flash[:danger] = "ログインしてください。"
122
+ redirect_to login_url
123
+ end
124
+ end
125
+
126
+ #管理者は勤怠編集画面の表示禁止
127
+ def hidden
128
+ if current_user.admin?
129
+ redirect_to(root_url)
130
+ end
131
+ end
132
+ end
69
133
  ```
134
+ ・users_controller.rb
135
+ ```ここに言語を入力
136
+ class UsersController < ApplicationController
137
+ before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :show]
138
+ before_action :correct_user, only: [:edit, :update]
139
+ before_action :admin_user, only: [:destroy, :edit_basic_info, :update_basic_info, :index]
140
+ before_action :general_user, only: :show
141
+ before_action :hidden, only: :show
142
+
143
+ def new
144
+ @user = User.new
145
+ end
146
+
147
+ def show
148
+ @user = User.find(params[:id])
149
+ if params[:first_day].nil?
150
+ @first_day = Date.today.beginning_of_month
151
+ else
152
+ @first_day = Date.parse(params[:first_day])
153
+ end
154
+ @last_day = @first_day.end_of_month
155
+ (@first_day..@last_day).each do |day|
156
+ unless @user.attendances.any? {|attendance| attendance.worked_on == day}
157
+ record = @user.attendances.build(worked_on: day)
158
+ record.save
159
+ end
160
+ end
161
+ @dates = user_attendances_month_date
162
+ @worked_sum = @dates.where.not(started_at: nil).count
163
+ respond_to do |format|
164
+ format.html
165
+ format.csv do
166
+ send_data render_to_string, filename: "#{@user.name}.csv", type: :csv
167
+ end
168
+ end
169
+ end
170
+
171
+ def create
172
+ @user = User.new(user_params)
173
+ if @user.save
174
+ log_in @user
175
+ flash[:success] = "ユーザーの新規作成に成功しました。"
176
+ redirect_to @user
177
+ else
178
+ render 'new'
179
+ end
180
+ registered_count = import_emails
181
+ redirect_to emails_path, notice: "#{registered_count}件登録しました"
182
+ end
183
+
184
+ def edit
185
+ @user = User.find(params[:id])
186
+ end
187
+
188
+ def update
189
+ @user = User.find(params[:id])
190
+ if @user.update_attributes(user_params)
191
+ flash[:success] = "ユーザー情報を更新しました。"
192
+ redirect_to @user
193
+ else
194
+ render 'edit'
195
+ end
196
+ end
197
+
198
+ def index
199
+ if params[:q] && params[:q].reject { |key, value| value.blank? }.present?
200
+ @q = User.ransack(search_params, activated_true: true)
201
+ @title = "検索結果"
202
+ else
203
+ @q = User.ransack(activated_true: true)
204
+ @title = "ユーザー一覧"
205
+ end
206
+ @users = @q.result.paginate(page: params[:page])
207
+ end
208
+
209
+ def destroy
210
+ User.find(params[:id]).destroy
211
+ flash[:success] = "削除しました。"
212
+ redirect_to users_url
213
+ end
214
+
215
+ def edit_index
216
+ @user = User.find(params[:id])
217
+ end
218
+
219
+ def update_index
220
+ @user = User.find(params[:id])
221
+ if @user.update_attributes(update_index_params)
222
+ flash[:success] = "更新しました。"
223
+ redirect_to users_url
224
+ else
225
+ render 'index'
226
+ end
227
+ end
228
+
229
+ private
230
+
231
+ def user_params
232
+ params.require(:user).permit(:name, :email, :department, :password, :password_confirmation)
233
+ end
234
+
235
+ def basic_info_params
236
+ params.require(:user).permit(:basic_time, :work_time)
237
+ end
238
+
239
+ def update_index_params
240
+ params.require(:user).permit(:name, :email, :department, :password, :password_confirmation, :basic_time, :work_time, :employee_number, :card_id)
241
+ end
242
+
243
+ # beforeアクション
244
+
245
+ # ログイン済みユーザーか確認
246
+ def logged_in_user
247
+ unless logged_in?
248
+ store_location
249
+ flash[:danger] = "ログインしてください。"
250
+ redirect_to login_url
251
+ end
252
+ end
253
+
254
+ # 正しいユーザーかどうか確認
255
+ def correct_user
256
+ @user = User.find(params[:id])
257
+ redirect_to(root_url) unless current_user?(@user)
258
+ end
259
+
260
+ # 管理者かどうか確認
261
+ def admin_user
262
+ if !current_user.admin?
263
+ redirect_to(root_url)
264
+ flash[:danger] = "管理者以外アクセスできません。"
265
+ end
266
+ end
267
+
268
+ #検索機能
269
+ def search_params
270
+ params.require(:q).permit(:name_cont)
271
+ end
272
+
273
+ #一般ユーザーで他のユーザーのURLを指定しても開けないようにする
274
+ def general_user
275
+ @user = User.find(params[:id])
276
+ if !current_user.admin? && !current_user?(@user)
277
+ redirect_to(root_url)
278
+ end
279
+ end
280
+
281
+ #管理者は勤怠画面の表示禁止
282
+ def hidden
283
+ if current_user.admin?
284
+ redirect_to(root_url)
285
+ end
286
+ end
287
+
288
+ def import_emails
289
+ # 登録処理前のレコード数
290
+ current_email_count = ::Email.count
291
+ emails = []
292
+ # windowsで作られたファイルに対応するので、encoding: "SJIS"を付けている
293
+ CSV.foreach(params[:emails_file].path, headers: true, encoding: "SJIS") do |row|
294
+ 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"],
295
+ work_time: row["designated_work_start_time"], work_end_time: row["designated_work_end_time"], admin: row["admin"], password_digest: row["password"] })
296
+ end
297
+ # importメソッドでバルクインサートできる
298
+ ::Email.import(emails)
299
+ # 何レコード登録できたかを返す
300
+ ::Email.count - current_email_count
301
+ end
302
+
303
+ end
304
+
305
+ ```
70
306
  ・schema.rb
71
307
  ```ここに言語を入力
72
308
  create_table "attendances", force: :cascade do |t|

1

修正

2019/06/08 13:54

投稿

ikt_erk122
ikt_erk122

スコア40

title CHANGED
@@ -1,1 +1,1 @@
1
- チェックボックスを押されているときに翌日の時間を足すようにしたい
1
+ チェックボックスを押されているときに翌日の時間を足すようにしたい
body CHANGED
@@ -1,46 +1,61 @@
1
1
  【やりたいこと】
2
- チェックボックスを押しきに翌日の時間を足す(+24時間)したい
2
+ 出社・退社時間を編集する機能を追加する勉強をしています。編集する際にチェックボックスを押して編集すると翌日に退社(24時間)とい機能を追加したいです。
3
3
 
4
4
  【困っていること】
5
5
  チェックボックスは実装できているが、翌日の時間を取得する方法や書き方を調べてもわからず押しても何も変わらない状態。
6
+ 自分の考えはDateクラスのDate.tomorrowを使えばいけるのかなと思っています。
6
7
 
7
8
  【質問】
8
9
  丸投げになって申し訳ないですがアドバイスいただけると幸いです。よろしくお願いします。
9
10
 
10
11
  ・attendances/edit.html.erb
11
12
  ```ここに言語を入力
13
+ <%= form_for(@user, url: update_attendances_path, method: :patch) do |f| %>
14
+ <table class = "table-bordered table-striped table-condensed">
15
+ <thead>
16
+ <tr>
17
+ <th>日付</th>
18
+ <th>曜日</th>
19
+ <th>出社時間</th>
20
+ <th>退社時間</th>
21
+ <th>翌日</th>
22
+ </tr>
23
+ </thead>
24
+ <tbody>
12
- <% @dates.each do |day| %>
25
+ <% @dates.each do |day| %>
13
- <%= fields_for "attendances[]", day do |af| %>
26
+ <%= fields_for "attendances[]", day do |af| %>
14
- <tr>
27
+ <tr>
15
- <td><%= day.worked_on.to_s(:date) %></td>
28
+ <td><%= day.worked_on.to_s(:date) %></td>
16
- <td><%= %w{日 月 火 水 木 金 土}[day.worked_on.wday] %></td>
29
+ <td><%= %w{日 月 火 水 木 金 土}[day.worked_on.wday] %></td>
17
- <td>
30
+ <td>
18
- <% if day.worked_on > Date.today %>
31
+ <% if day.worked_on > Date.today %>
19
- <%= af.time_field :started_at, :readonly => true, class: "form-control" %>
32
+ <%= af.time_field :started_at, :readonly => true, class: "form-control" %>
20
- <% else %>
33
+ <% else %>
21
- <%= af.time_field :started_at, class: "form-control" %>
34
+ <%= af.time_field :started_at, class: "form-control" %>
22
- <% end %>
35
+ <% end %>
23
- </td>
36
+ </td>
24
- <td>
37
+ <td>
25
- <% if day.worked_on > Date.today %>
38
+ <% if day.worked_on > Date.today %>
26
- <%= af.time_field :finished_at, :readonly => true, class: "form-control" %>
39
+ <%= af.time_field :finished_at, :readonly => true, class: "form-control" %>
27
- <% else %>
40
+ <% else %>
28
- <%= af.time_field :finished_at, class: "form-control" %>
41
+ <%= af.time_field :finished_at, class: "form-control" %>
29
- <% end %>
42
+ <% end %>
30
- </td>
43
+ </td>
31
- <td>
44
+ <td>
32
- <% if day.worked_on > Date.today %>
45
+ <% if day.worked_on > Date.today %>
33
- <% else %>
46
+ <% else %>
34
- <%= af.check_box :next_day, {}, "true", "false" %>
47
+ <%= af.check_box :next_day, {}, "true", "false" %>
48
+ <% end %>
49
+ </td>
50
+ </tr>
51
+ <% end %>
35
52
  <% end %>
36
- </td>
53
+ </tbody>
37
- <td>
38
- <% if day.started_at.present? && day.finished_at.present? %>
39
- <%= working_times(day.started_at, day.finished_at) %>
40
- <% end %>
54
+ </table>
55
+ <div class="btn-attendances-update">
56
+ <%= f.submit "更新", class: "btn btn-primary" %>
57
+ <%= link_to "キャンセル", user_path(@user, params:{first_day: @first_day }), class: "btn btn-default btn-block" %>
41
- </td>
58
+ </div>
42
- <td><%= af.text_field :note, class: "form-control" %></td>
43
- </tr>
44
59
  <% end %>
45
60
  ```
46
61
  ・attedances_controller.rb
@@ -52,4 +67,18 @@
52
67
  @dates = user_attendances_month_date
53
68
  end
54
69
  ```
70
+ ・schema.rb
71
+ ```ここに言語を入力
72
+ create_table "attendances", force: :cascade do |t|
73
+ t.date "worked_on"
74
+ t.datetime "started_at"
75
+ t.datetime "finished_at"
76
+ t.string "note"
77
+ t.integer "user_id"
78
+ t.datetime "created_at", null: false
79
+ t.datetime "updated_at", null: false
80
+ t.datetime "next_day"
81
+ t.index ["user_id"], name: "index_attendances_on_user_id"
82
+ end
83
+ ```
55
- ![![イメージ説明](37c937b30c5fb0f826772af164928aca.png)]
84
+ ![イメージ説明](a79515b419c9efc3d4eb9c6fd133cf46.png)