質問編集履歴
1
ソースコードとエラーの内容が異なっていたため、修正致しました。(ActionActivationControllerのeditアクションの部分)
test
CHANGED
File without changes
|
test
CHANGED
@@ -52,6 +52,190 @@
|
|
52
52
|
|
53
53
|
if user && !user.activated? && user.authenticated?(:activation, params[:id])
|
54
54
|
|
55
|
+
user.activate
|
56
|
+
|
57
|
+
log_in user
|
58
|
+
|
59
|
+
flash[:success] = "Account activated!"
|
60
|
+
|
61
|
+
redirect_to user
|
62
|
+
|
63
|
+
else
|
64
|
+
|
65
|
+
flash[:danger] = "Invalid activation link"
|
66
|
+
|
67
|
+
redirect_to root_url
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
```
|
76
|
+
|
77
|
+
```
|
78
|
+
|
79
|
+
#AccountActivationsControllerに関係するモデルです。
|
80
|
+
|
81
|
+
#authenticated?メソッドとactivateメソッドの情報が必要だと思い、掲載させていただきました。
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
class User < ApplicationRecord
|
86
|
+
|
87
|
+
attr_accessor :remember_token,:activation_token
|
88
|
+
|
89
|
+
before_save :downcase_email
|
90
|
+
|
91
|
+
before_create :create_activation_digest
|
92
|
+
|
93
|
+
validates :name, presence: true, length: { maximum: 50 }
|
94
|
+
|
95
|
+
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i
|
96
|
+
|
97
|
+
validates :email, presence: true, length: { maximum: 255 },
|
98
|
+
|
99
|
+
format: { with: VALID_EMAIL_REGEX },
|
100
|
+
|
101
|
+
uniqueness: true
|
102
|
+
|
103
|
+
has_secure_password
|
104
|
+
|
105
|
+
validates :password, presence: true, length: { minimum: 6 },allow_nil: true
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
# 渡された文字列のハッシュ値を返す
|
110
|
+
|
111
|
+
def User.digest(string)
|
112
|
+
|
113
|
+
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
|
114
|
+
|
115
|
+
BCrypt::Engine.cost
|
116
|
+
|
117
|
+
BCrypt::Password.create(string, cost: cost)
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
# ランダムなトークンを返す
|
124
|
+
|
125
|
+
def User.new_token
|
126
|
+
|
127
|
+
SecureRandom.urlsafe_base64
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
# 永続セッションのためにユーザーをデータベースに記憶する
|
134
|
+
|
135
|
+
def remember
|
136
|
+
|
137
|
+
self.remember_token = User.new_token
|
138
|
+
|
139
|
+
update_attribute(:remember_digest, User.digest(remember_token))
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
# 渡されたトークンがダイジェストと一致したらtrueを返す
|
146
|
+
|
147
|
+
def authenticated?(attribute, token)
|
148
|
+
|
149
|
+
digest = send("#{attribute}_digest")
|
150
|
+
|
151
|
+
return false if digest.nil?
|
152
|
+
|
153
|
+
BCrypt::Password.new(digest).is_password?(token)
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
def forget
|
160
|
+
|
161
|
+
update_attribute(:remember_digest, nil)
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
def activate
|
168
|
+
|
169
|
+
update_attribute(:activated, true)
|
170
|
+
|
171
|
+
update_attribute(:activated_at, Time.zone.now)
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
# 有効化用のメールを送信する
|
178
|
+
|
179
|
+
def send_activation_email
|
180
|
+
|
181
|
+
UserMailer.account_activation(self).deliver_now
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
private
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
# メールアドレスをすべて小文字にする
|
192
|
+
|
193
|
+
def downcase_email
|
194
|
+
|
195
|
+
self.email = email.downcase
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
# 有効化トークンとダイジェストを作成および代入する
|
202
|
+
|
203
|
+
def create_activation_digest
|
204
|
+
|
205
|
+
self.activation_token = User.new_token
|
206
|
+
|
207
|
+
self.activation_digest = User.digest(activation_token)
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
```
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
###試したこと
|
218
|
+
|
219
|
+
editアクションのif文の1行目
|
220
|
+
|
221
|
+
`if user && !user.activated? && user.authenticated?(:activation, params[:id])`
|
222
|
+
|
223
|
+
が強制的に成功するように書き換えて見た。
|
224
|
+
|
225
|
+
( `if user == user`にして見た。 )
|
226
|
+
|
227
|
+
```
|
228
|
+
|
229
|
+
class AccountActivationsController < ApplicationController
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
def edit
|
234
|
+
|
235
|
+
user = User.find_by(email: params[:email])
|
236
|
+
|
237
|
+
if user == user
|
238
|
+
|
55
239
|
user.update_attribute(:activated, true)
|
56
240
|
|
57
241
|
user.update_attribute(:activated_at, Time.zone.now)
|
@@ -76,192 +260,6 @@
|
|
76
260
|
|
77
261
|
```
|
78
262
|
|
79
|
-
```
|
80
|
-
|
81
|
-
#AccountActivationsControllerに関係するモデルです。
|
82
|
-
|
83
|
-
#authenticated?メソッドとactivateメソッドの情報が必要だと思い、掲載させていただきました。
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
class User < ApplicationRecord
|
88
|
-
|
89
|
-
attr_accessor :remember_token,:activation_token
|
90
|
-
|
91
|
-
before_save :downcase_email
|
92
|
-
|
93
|
-
before_create :create_activation_digest
|
94
|
-
|
95
|
-
validates :name, presence: true, length: { maximum: 50 }
|
96
|
-
|
97
|
-
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i
|
98
|
-
|
99
|
-
validates :email, presence: true, length: { maximum: 255 },
|
100
|
-
|
101
|
-
format: { with: VALID_EMAIL_REGEX },
|
102
|
-
|
103
|
-
uniqueness: true
|
104
|
-
|
105
|
-
has_secure_password
|
106
|
-
|
107
|
-
validates :password, presence: true, length: { minimum: 6 },allow_nil: true
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
# 渡された文字列のハッシュ値を返す
|
112
|
-
|
113
|
-
def User.digest(string)
|
114
|
-
|
115
|
-
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
|
116
|
-
|
117
|
-
BCrypt::Engine.cost
|
118
|
-
|
119
|
-
BCrypt::Password.create(string, cost: cost)
|
120
|
-
|
121
|
-
end
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
# ランダムなトークンを返す
|
126
|
-
|
127
|
-
def User.new_token
|
128
|
-
|
129
|
-
SecureRandom.urlsafe_base64
|
130
|
-
|
131
|
-
end
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
# 永続セッションのためにユーザーをデータベースに記憶する
|
136
|
-
|
137
|
-
def remember
|
138
|
-
|
139
|
-
self.remember_token = User.new_token
|
140
|
-
|
141
|
-
update_attribute(:remember_digest, User.digest(remember_token))
|
142
|
-
|
143
|
-
end
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
# 渡されたトークンがダイジェストと一致したらtrueを返す
|
148
|
-
|
149
|
-
def authenticated?(attribute, token)
|
150
|
-
|
151
|
-
digest = send("#{attribute}_digest")
|
152
|
-
|
153
|
-
return false if digest.nil?
|
154
|
-
|
155
|
-
BCrypt::Password.new(digest).is_password?(token)
|
156
|
-
|
157
|
-
end
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
def forget
|
162
|
-
|
163
|
-
update_attribute(:remember_digest, nil)
|
164
|
-
|
165
|
-
end
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
def activate
|
170
|
-
|
171
|
-
update_attribute(:activated, true)
|
172
|
-
|
173
|
-
update_attribute(:activated_at, Time.zone.now)
|
174
|
-
|
175
|
-
end
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
# 有効化用のメールを送信する
|
180
|
-
|
181
|
-
def send_activation_email
|
182
|
-
|
183
|
-
UserMailer.account_activation(self).deliver_now
|
184
|
-
|
185
|
-
end
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
private
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
# メールアドレスをすべて小文字にする
|
194
|
-
|
195
|
-
def downcase_email
|
196
|
-
|
197
|
-
self.email = email.downcase
|
198
|
-
|
199
|
-
end
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
# 有効化トークンとダイジェストを作成および代入する
|
204
|
-
|
205
|
-
def create_activation_digest
|
206
|
-
|
207
|
-
self.activation_token = User.new_token
|
208
|
-
|
209
|
-
self.activation_digest = User.digest(activation_token)
|
210
|
-
|
211
|
-
end
|
212
|
-
|
213
|
-
end
|
214
|
-
|
215
|
-
```
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
###試したこと
|
220
|
-
|
221
|
-
editアクションのif文の1行目
|
222
|
-
|
223
|
-
`if user && !user.activated? && user.authenticated?(:activation, params[:id])`
|
224
|
-
|
225
|
-
が強制的に成功するように書き換えて見た。
|
226
|
-
|
227
|
-
( `if user == user`にして見た。 )
|
228
|
-
|
229
|
-
```
|
230
|
-
|
231
|
-
class AccountActivationsController < ApplicationController
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
def edit
|
236
|
-
|
237
|
-
user = User.find_by(email: params[:email])
|
238
|
-
|
239
|
-
if user == user
|
240
|
-
|
241
|
-
user.update_attribute(:activated, true)
|
242
|
-
|
243
|
-
user.update_attribute(:activated_at, Time.zone.now)
|
244
|
-
|
245
|
-
log_in user
|
246
|
-
|
247
|
-
flash[:success] = "Account activated!"
|
248
|
-
|
249
|
-
redirect_to user
|
250
|
-
|
251
|
-
else
|
252
|
-
|
253
|
-
flash[:danger] = "Invalid activation link"
|
254
|
-
|
255
|
-
redirect_to root_url
|
256
|
-
|
257
|
-
end
|
258
|
-
|
259
|
-
end
|
260
|
-
|
261
|
-
end
|
262
|
-
|
263
|
-
```
|
264
|
-
|
265
263
|
こうした結果、下の画像のようなエラーが出ました。
|
266
264
|
|
267
265
|
userに原因があるのかなと考えているのですが、どういった原因があるのか検討がつきません。。
|