質問編集履歴

2

文言修正

2017/07/25 14:14

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -12,12 +12,236 @@
12
12
 
13
13
 
14
14
 
15
-
15
+ ```ここに言語を入力
16
+
17
+ class SignupController < ApplicationController
18
+
19
+ layout 'info_get'
20
+
21
+ protect_from_forgery
22
+
23
+
24
+
25
+ include SessionManagement
26
+
27
+ before_action :is_session, only: [:index, :confirm]
28
+
29
+ before_action :sign_in!, only: [:complete]
30
+
31
+
32
+
33
+ def index
34
+
35
+ @title = 'ユーザー情報入力'
36
+
37
+ @errMsg = Hash.new
38
+
39
+
40
+
41
+ dalli = Dalli::Client.new('localhost:11211')
42
+
43
+
44
+
45
+ if request.get? then
46
+
47
+ @user = User.new
48
+
49
+ if dalli.get('signup_user') != nil then
50
+
51
+ signup_user = dalli.get('signup_user')
52
+
53
+ @user.name = signup_user.name
54
+
55
+ @user.email = signup_user.email
56
+
57
+ end
58
+
59
+ else
60
+
61
+ @user = User.new user_params
62
+
63
+
64
+
65
+ if @user.valid? then
66
+
67
+ dalli.set('signup_user', @user)
68
+
69
+ redirect_to '/signup/confirm'
70
+
71
+ else
72
+
73
+ #エラーメッセージの連想配列を生成
74
+
75
+ @user.errors.messages.each do |key, val|
76
+
77
+ if key == :password_confirmation && !@errMsg.has_key?(:password) then
78
+
79
+ @errMsg[:password] = val[0]
80
+
81
+ next
82
+
83
+ end
84
+
85
+ @errMsg[key] = val[0]
86
+
87
+ end
88
+
89
+ end
90
+
91
+ end
92
+
93
+ end
94
+
95
+
96
+
97
+ def confirm
98
+
99
+ @title = 'ユーザー情報入力確認'
100
+
101
+ dalli = Dalli::Client.new('localhost:11211')
102
+
103
+
104
+
105
+ if request.post? then
106
+
107
+ @user = User.new user_params
108
+
109
+ if @user.save(validate: false) then
110
+
111
+ login_user = User.find_by(email: @user.email)
112
+
113
+
114
+
115
+ if login_user && login_user.authenticate(@user.password) then
116
+
117
+ dalli.delete('signup_user')
118
+
119
+ reset_session
120
+
121
+ dalli.set('login_user', login_user)
122
+
123
+ redirect_to '/signup/complete'
124
+
125
+ end
126
+
127
+ else
128
+
129
+
130
+
131
+ end
132
+
133
+ redirect_to '/signup/confirm'
134
+
135
+ else
136
+
137
+ @user = dalli.get('signup_user')
138
+
139
+ end
140
+
141
+ end
142
+
143
+
144
+
145
+ def complete
146
+
147
+ @title = 'ユーザー情報登録完了'
148
+
149
+ end
150
+
151
+
152
+
153
+ private
154
+
155
+ def user_params
156
+
157
+ params.require(:user).permit(:name, :email, :password, :password_confirmation)
158
+
159
+ end
160
+
161
+
162
+
163
+ end
164
+
165
+ ```
166
+
167
+ 確認画面でフォームを実装しているコードは下記になります。
168
+
169
+ ```ここに言語を入力
170
+
171
+ <%= form_for(@user, url:{controller: 'signup', action: 'confirm'}) do |f| %>
172
+
173
+ <%= f.hidden_field :name %>
174
+
175
+ <%= f.hidden_field :email %>
176
+
177
+ <%= f.hidden_field :password %>
178
+
179
+ <%= f.hidden_field :password_confirmation %>
180
+
181
+ <input type="button" value="戻る" onClick="location.href='/signup'">
182
+
183
+ <%= f.submit '登録' %>
184
+
185
+ <% end %>
186
+
187
+ ```
188
+
189
+
190
+
191
+ DBテーブルのスキーマは下記の通りです。
192
+
193
+ ```ここに言語を入力
194
+
195
+ create_table "users", force: :cascade do |t|
196
+
197
+ t.text "name"
198
+
199
+ t.text "email"
200
+
201
+ t.text "password_digest"
202
+
203
+ t.text "kari_password"
204
+
205
+ t.text "kari_created_at"
206
+
207
+ t.text "session_id"
208
+
209
+ t.text "lastlogin_at"
210
+
211
+ t.datetime "created_at", null: false
212
+
213
+ t.datetime "updated_at", null: false
214
+
215
+ end
216
+
217
+ ```
218
+
219
+
220
+
221
+ 前提として、scaffoldを使用していません。
222
+
223
+ また、セッション管理の為にmemchachedを使用しています。
224
+
225
+
226
+
227
+
228
+
229
+ どのように対処したらよいのでしょうか。
230
+
231
+
232
+
233
+
234
+
235
+ -------------------------------------
16
236
 
17
237
  2017/7/25追記
18
238
 
19
239
  下記の修正を施しました。
20
240
 
241
+
242
+
243
+ ■修正後のコントローラー
244
+
21
245
  ```ここに言語を入力
22
246
 
23
247
  class SignupController < ApplicationController
@@ -42,19 +266,17 @@
42
266
 
43
267
  @errMsg = Hash.new
44
268
 
45
-
46
-
47
- dalli = Dalli::Client.new('localhost:11211')
48
-
49
269
 
50
270
 
51
271
  if request.get? then
52
272
 
53
273
  @user = User.new
54
274
 
55
- if dalli.get('signup_user') != nil then
275
+ if Rails.cache.exist?('signup_user') then
56
-
276
+
57
- signup_user = dalli.get('signup_user')
277
+ signup_user = Rails.cache.read('signup_user')
278
+
279
+
58
280
 
59
281
  @user.name = signup_user.name
60
282
 
@@ -70,7 +292,7 @@
70
292
 
71
293
  if @user.valid? then
72
294
 
73
- dalli.set('signup_user', @user)
295
+ Rails.cache.write('signup_user', @user)
74
296
 
75
297
  redirect_to '/signup/confirm'
76
298
 
@@ -104,13 +326,11 @@
104
326
 
105
327
  @title = 'ユーザー情報入力確認'
106
328
 
107
- dalli = Dalli::Client.new('localhost:11211')
329
+
108
-
109
-
110
330
 
111
331
  if request.post? then
112
332
 
113
- @user = User.new user_params
333
+ @user = Rails.cache.read('signup_user')
114
334
 
115
335
  if @user.save(validate: false) then
116
336
 
@@ -120,18 +340,14 @@
120
340
 
121
341
  if login_user && login_user.authenticate(@user.password) then
122
342
 
123
- dalli.delete('signup_user')
343
+ Rails.cache.delete('signup_user')
124
-
125
- reset_session
344
+
126
-
127
- dalli.set('login_user', login_user)
345
+ Rails.cache.write('login_user', login_user)
128
346
 
129
347
  redirect_to '/signup/complete'
130
348
 
131
349
  end
132
350
 
133
- else
134
-
135
351
 
136
352
 
137
353
  end
@@ -140,7 +356,7 @@
140
356
 
141
357
  else
142
358
 
143
- @user = dalli.get('signup_user')
359
+ @user = Rails.cache.read('signup_user')
144
360
 
145
361
  end
146
362
 
@@ -168,250 +384,38 @@
168
384
 
169
385
  end
170
386
 
387
+
388
+
171
389
  ```
172
390
 
391
+
392
+
173
- 確認画面でフォームを実装しているコードは下記になります。
393
+ ■userモデル
174
394
 
175
395
  ```ここに言語を入力
176
396
 
177
- <%= form_for(@user, url:{controller: 'signup', action: 'confirm'}) do |f| %>
397
+ class User < ApplicationRecord
178
-
398
+
179
- <%= f.hidden_field :name %>
399
+ include ActiveModel::Model
180
-
181
- <%= f.hidden_field :email %>
400
+
182
-
401
+
402
+
183
- <%= f.hidden_field :password %>
403
+ has_secure_password validations: false
184
-
404
+
405
+
406
+
185
- <%= f.hidden_field :password_confirmation %>
407
+ attr_accessor :name, :email, :password, :password_confirmation
186
-
187
- <input type="button" value="戻る" onClick="location.href='/signup'">
408
+
188
-
409
+
410
+
189
- <%= f.submit '登録' %>
411
+ validates :name, presence: {message: 'ユーザー名を入力して下さい。'}
412
+
190
-
413
+ validates :email, presence: {message: 'Emailを入力して下さい。'}
414
+
415
+ validates :password, presence: {message: 'パスワードを入力して下さい。'}
416
+
191
- <% end %>
417
+ end
418
+
419
+
192
420
 
193
421
  ```
194
-
195
-
196
-
197
- DBテーブルのスキーマは下記の通りです。
198
-
199
- ```ここに言語を入力
200
-
201
- create_table "users", force: :cascade do |t|
202
-
203
- t.text "name"
204
-
205
- t.text "email"
206
-
207
- t.text "password_digest"
208
-
209
- t.text "kari_password"
210
-
211
- t.text "kari_created_at"
212
-
213
- t.text "session_id"
214
-
215
- t.text "lastlogin_at"
216
-
217
- t.datetime "created_at", null: false
218
-
219
- t.datetime "updated_at", null: false
220
-
221
- end
222
-
223
- ```
224
-
225
-
226
-
227
- 前提として、scaffoldを使用していません。
228
-
229
- また、セッション管理の為にmemchachedを使用しています。
230
-
231
-
232
-
233
-
234
-
235
- どのように対処したらよいのでしょうか。
236
-
237
-
238
-
239
- ■修正後のコントローラー
240
-
241
- ```ここに言語を入力
242
-
243
- class SignupController < ApplicationController
244
-
245
- layout 'info_get'
246
-
247
- protect_from_forgery
248
-
249
-
250
-
251
- include SessionManagement
252
-
253
- before_action :is_session, only: [:index, :confirm]
254
-
255
- before_action :sign_in!, only: [:complete]
256
-
257
-
258
-
259
- def index
260
-
261
- @title = 'ユーザー情報入力'
262
-
263
- @errMsg = Hash.new
264
-
265
-
266
-
267
- if request.get? then
268
-
269
- @user = User.new
270
-
271
- if Rails.cache.exist?('signup_user') then
272
-
273
- signup_user = Rails.cache.read('signup_user')
274
-
275
-
276
-
277
- @user.name = signup_user.name
278
-
279
- @user.email = signup_user.email
280
-
281
- end
282
-
283
- else
284
-
285
- @user = User.new user_params
286
-
287
-
288
-
289
- if @user.valid? then
290
-
291
- Rails.cache.write('signup_user', @user)
292
-
293
- redirect_to '/signup/confirm'
294
-
295
- else
296
-
297
- #エラーメッセージの連想配列を生成
298
-
299
- @user.errors.messages.each do |key, val|
300
-
301
- if key == :password_confirmation && !@errMsg.has_key?(:password) then
302
-
303
- @errMsg[:password] = val[0]
304
-
305
- next
306
-
307
- end
308
-
309
- @errMsg[key] = val[0]
310
-
311
- end
312
-
313
- end
314
-
315
- end
316
-
317
- end
318
-
319
-
320
-
321
- def confirm
322
-
323
- @title = 'ユーザー情報入力確認'
324
-
325
-
326
-
327
- if request.post? then
328
-
329
- @user = Rails.cache.read('signup_user')
330
-
331
- if @user.save(validate: false) then
332
-
333
- login_user = User.find_by(email: @user.email)
334
-
335
-
336
-
337
- if login_user && login_user.authenticate(@user.password) then
338
-
339
- Rails.cache.delete('signup_user')
340
-
341
- Rails.cache.write('login_user', login_user)
342
-
343
- redirect_to '/signup/complete'
344
-
345
- end
346
-
347
-
348
-
349
- end
350
-
351
- redirect_to '/signup/confirm'
352
-
353
- else
354
-
355
- @user = Rails.cache.read('signup_user')
356
-
357
- end
358
-
359
- end
360
-
361
-
362
-
363
- def complete
364
-
365
- @title = 'ユーザー情報登録完了'
366
-
367
- end
368
-
369
-
370
-
371
- private
372
-
373
- def user_params
374
-
375
- params.require(:user).permit(:name, :email, :password, :password_confirmation)
376
-
377
- end
378
-
379
-
380
-
381
- end
382
-
383
-
384
-
385
- ```
386
-
387
-
388
-
389
- ■userモデル
390
-
391
- ```ここに言語を入力
392
-
393
- class User < ApplicationRecord
394
-
395
- include ActiveModel::Model
396
-
397
-
398
-
399
- has_secure_password validations: false
400
-
401
-
402
-
403
- attr_accessor :name, :email, :password, :password_confirmation
404
-
405
-
406
-
407
- validates :name, presence: {message: 'ユーザー名を入力して下さい。'}
408
-
409
- validates :email, presence: {message: 'Emailを入力して下さい。'}
410
-
411
- validates :password, presence: {message: 'パスワードを入力して下さい。'}
412
-
413
- end
414
-
415
-
416
-
417
- ```

1

回答頂いた内容を踏まえてコントローラーを修正、userモデルのコード追加

2017/07/25 14:14

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -12,6 +12,12 @@
12
12
 
13
13
 
14
14
 
15
+
16
+
17
+ 2017/7/25追記
18
+
19
+ 下記の修正を施しました。
20
+
15
21
  ```ここに言語を入力
16
22
 
17
23
  class SignupController < ApplicationController
@@ -227,3 +233,185 @@
227
233
 
228
234
 
229
235
  どのように対処したらよいのでしょうか。
236
+
237
+
238
+
239
+ ■修正後のコントローラー
240
+
241
+ ```ここに言語を入力
242
+
243
+ class SignupController < ApplicationController
244
+
245
+ layout 'info_get'
246
+
247
+ protect_from_forgery
248
+
249
+
250
+
251
+ include SessionManagement
252
+
253
+ before_action :is_session, only: [:index, :confirm]
254
+
255
+ before_action :sign_in!, only: [:complete]
256
+
257
+
258
+
259
+ def index
260
+
261
+ @title = 'ユーザー情報入力'
262
+
263
+ @errMsg = Hash.new
264
+
265
+
266
+
267
+ if request.get? then
268
+
269
+ @user = User.new
270
+
271
+ if Rails.cache.exist?('signup_user') then
272
+
273
+ signup_user = Rails.cache.read('signup_user')
274
+
275
+
276
+
277
+ @user.name = signup_user.name
278
+
279
+ @user.email = signup_user.email
280
+
281
+ end
282
+
283
+ else
284
+
285
+ @user = User.new user_params
286
+
287
+
288
+
289
+ if @user.valid? then
290
+
291
+ Rails.cache.write('signup_user', @user)
292
+
293
+ redirect_to '/signup/confirm'
294
+
295
+ else
296
+
297
+ #エラーメッセージの連想配列を生成
298
+
299
+ @user.errors.messages.each do |key, val|
300
+
301
+ if key == :password_confirmation && !@errMsg.has_key?(:password) then
302
+
303
+ @errMsg[:password] = val[0]
304
+
305
+ next
306
+
307
+ end
308
+
309
+ @errMsg[key] = val[0]
310
+
311
+ end
312
+
313
+ end
314
+
315
+ end
316
+
317
+ end
318
+
319
+
320
+
321
+ def confirm
322
+
323
+ @title = 'ユーザー情報入力確認'
324
+
325
+
326
+
327
+ if request.post? then
328
+
329
+ @user = Rails.cache.read('signup_user')
330
+
331
+ if @user.save(validate: false) then
332
+
333
+ login_user = User.find_by(email: @user.email)
334
+
335
+
336
+
337
+ if login_user && login_user.authenticate(@user.password) then
338
+
339
+ Rails.cache.delete('signup_user')
340
+
341
+ Rails.cache.write('login_user', login_user)
342
+
343
+ redirect_to '/signup/complete'
344
+
345
+ end
346
+
347
+
348
+
349
+ end
350
+
351
+ redirect_to '/signup/confirm'
352
+
353
+ else
354
+
355
+ @user = Rails.cache.read('signup_user')
356
+
357
+ end
358
+
359
+ end
360
+
361
+
362
+
363
+ def complete
364
+
365
+ @title = 'ユーザー情報登録完了'
366
+
367
+ end
368
+
369
+
370
+
371
+ private
372
+
373
+ def user_params
374
+
375
+ params.require(:user).permit(:name, :email, :password, :password_confirmation)
376
+
377
+ end
378
+
379
+
380
+
381
+ end
382
+
383
+
384
+
385
+ ```
386
+
387
+
388
+
389
+ ■userモデル
390
+
391
+ ```ここに言語を入力
392
+
393
+ class User < ApplicationRecord
394
+
395
+ include ActiveModel::Model
396
+
397
+
398
+
399
+ has_secure_password validations: false
400
+
401
+
402
+
403
+ attr_accessor :name, :email, :password, :password_confirmation
404
+
405
+
406
+
407
+ validates :name, presence: {message: 'ユーザー名を入力して下さい。'}
408
+
409
+ validates :email, presence: {message: 'Emailを入力して下さい。'}
410
+
411
+ validates :password, presence: {message: 'パスワードを入力して下さい。'}
412
+
413
+ end
414
+
415
+
416
+
417
+ ```