質問編集履歴
2
エラーの詳細
test
CHANGED
File without changes
|
test
CHANGED
@@ -231,3 +231,7 @@
|
|
231
231
|
|
232
232
|
|
233
233
|
これらのlast_login_atをlogin_atに、last_logout_atをlogout_atに、last_login_from_ip_addressをlogin_from_ip_addressに移す処理を行なっています。
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
エラーの内容はundefined method `id' for nil:NilClassです。
|
1
ソースコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -11,3 +11,223 @@
|
|
11
11
|
|
12
12
|
|
13
13
|
※本当は管理者テーブルを別に作った方がifとか使わなくても綺麗なんでしょうが、そこは質問の本筋とは違うので一旦保留にしてます。。。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
モデルはこのようになってます。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
user.rb
|
22
|
+
|
23
|
+
```
|
24
|
+
|
25
|
+
class User < ApplicationRecord
|
26
|
+
|
27
|
+
authenticates_with_sorcery!
|
28
|
+
|
29
|
+
has_many :working_hours, dependent: :destroy
|
30
|
+
|
31
|
+
has_many :login_logs, dependent: :destroy
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
validates :password, length: { minimum: 3 }, if: -> { new_record? || changes[:crypted_password] }
|
36
|
+
|
37
|
+
validates :password, confirmation: true, if: -> { new_record? || changes[:crypted_password] }
|
38
|
+
|
39
|
+
validates :password_confirmation, presence: true, if: -> { new_record? || changes[:crypted_password] }
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
enum usertype: {admin: 0, user: 1}
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
working_hour.rb
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
class LoginLog < ApplicationRecord
|
56
|
+
|
57
|
+
belongs_to :user
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
login_log.rb
|
64
|
+
|
65
|
+
```
|
66
|
+
|
67
|
+
class LoginLog < ApplicationRecord
|
68
|
+
|
69
|
+
belongs_to :user
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
```
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
working_hourテーブルは
|
78
|
+
|
79
|
+
・user_id
|
80
|
+
|
81
|
+
・start_datetime
|
82
|
+
|
83
|
+
・finish_datetime
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
login_logテーブルは
|
88
|
+
|
89
|
+
・user_id
|
90
|
+
|
91
|
+
・login_at
|
92
|
+
|
93
|
+
・logout_at
|
94
|
+
|
95
|
+
・login_from_ip_address
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
をそれぞれ持っていて
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
working_hours_controller.rbは
|
104
|
+
|
105
|
+
```
|
106
|
+
|
107
|
+
class WorkingHoursController < ApplicationController
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
def register_work_starting_datetime
|
112
|
+
|
113
|
+
working_hour = WorkingHour.create(user_id: current_user.id, start_datetime: Time.current)
|
114
|
+
|
115
|
+
redirect_to controller: "users", action: "show", id: current_user.id
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
def register_work_finished_datetime
|
122
|
+
|
123
|
+
working_hour = WorkingHour.where(user_id: current_user.id, finish_datetime: nil).last
|
124
|
+
|
125
|
+
working_hour.finish_datetime = Time.current
|
126
|
+
|
127
|
+
working_hour.save
|
128
|
+
|
129
|
+
redirect_to controller: "users", action: "show", id: current_user.id
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
で、作業ボタンをviewに設置して、そのタイミングで開始時間終了時間を記録する流れになってます。
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
login_logs_controller.rbはなくて、application_controllerの中で
|
144
|
+
|
145
|
+
```
|
146
|
+
|
147
|
+
class ApplicationController < ActionController::Base
|
148
|
+
|
149
|
+
protect_from_forgery with: :exception
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
protected
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
#sorceryのモジュール"activity_logging"のメソッドを上書き
|
158
|
+
|
159
|
+
#https://github.com/Sorcery/sorcery/blob/master/lib/sorcery/controller/submodules/activity_logging.rb
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
def register_login_time_to_db(user, credentials)
|
164
|
+
|
165
|
+
super(user, credentials)
|
166
|
+
|
167
|
+
LoginLog.create(user_id: current_user.id, login_at: user.last_login_at)
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
def register_logout_time_to_db
|
174
|
+
|
175
|
+
super
|
176
|
+
|
177
|
+
login_log = LoginLog.where(user_id: current_user.id, logout_at: nil).last
|
178
|
+
|
179
|
+
login_log.logout_at = current_user.last_logout_at
|
180
|
+
|
181
|
+
login_log.save
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
def register_last_ip_address(user, credentials)
|
188
|
+
|
189
|
+
super(user, credentials)
|
190
|
+
|
191
|
+
login_log = LoginLog.where(user_id: current_user.id, login_from_ip_address: nil).last
|
192
|
+
|
193
|
+
login_log.login_from_ip_address = current_user.last_login_from_ip_address
|
194
|
+
|
195
|
+
login_log.save
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
```
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
もともとsorceryには活動時間を記録するモジュールがあり、それを上書きする形で
|
206
|
+
|
207
|
+
```
|
208
|
+
|
209
|
+
class SorceryActivityLogging < ActiveRecord::Migration[5.0]
|
210
|
+
|
211
|
+
def change
|
212
|
+
|
213
|
+
add_column :users, :last_login_at, :datetime, :default => nil
|
214
|
+
|
215
|
+
add_column :users, :last_logout_at, :datetime, :default => nil
|
216
|
+
|
217
|
+
add_column :users, :last_activity_at, :datetime, :default => nil
|
218
|
+
|
219
|
+
add_column :users, :last_login_from_ip_address, :string, :default => nil
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
add_index :users, [:last_logout_at, :last_activity_at]
|
224
|
+
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
```
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
これらのlast_login_atをlogin_atに、last_logout_atをlogout_atに、last_login_from_ip_addressをlogin_from_ip_addressに移す処理を行なっています。
|