質問編集履歴
6
説明追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -332,6 +332,108 @@
|
|
332
332
|
|
333
333
|
```
|
334
334
|
|
335
|
+
application_controller
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
class ApplicationController < ActionController::Base
|
340
|
+
|
341
|
+
before_action :set_current_user
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
def top
|
348
|
+
|
349
|
+
end
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
def set_current_user
|
356
|
+
|
357
|
+
@current_user = User.find_by(id: session[:user_id])
|
358
|
+
|
359
|
+
end
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
def authenticate_user
|
366
|
+
|
367
|
+
end
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
def forbid_login_user
|
374
|
+
|
375
|
+
if @current_user
|
376
|
+
|
377
|
+
flash[:notice] = "すでにログインしています"
|
378
|
+
|
379
|
+
redirect_to("/posts/index")
|
380
|
+
|
381
|
+
end
|
382
|
+
|
383
|
+
end
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
end
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
```
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
```
|
400
|
+
|
401
|
+
home_controller
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
class HomeController < ApplicationController
|
406
|
+
|
407
|
+
before_action :forbid_login_user, {only: [:new, :create, :login_form, :login]}
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
def top
|
416
|
+
|
417
|
+
end
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
end
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
```
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
```
|
436
|
+
|
335
437
|
users/login_form.html.erb
|
336
438
|
|
337
439
|
|
5
説明追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -516,6 +516,80 @@
|
|
516
516
|
|
517
517
|
|
518
518
|
|
519
|
+
```
|
520
|
+
|
521
|
+
user/edit
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
<div class="user-index">
|
526
|
+
|
527
|
+
<h1>ユーザー情報の編集</h1>
|
528
|
+
|
529
|
+
<% @user.errors.full_messages.each do |message| %>
|
530
|
+
|
531
|
+
<div class="form-error">
|
532
|
+
|
533
|
+
<%= message %>
|
534
|
+
|
535
|
+
</div>
|
536
|
+
|
537
|
+
<% end %>
|
538
|
+
|
539
|
+
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
<div class="user-edit-container">
|
544
|
+
|
545
|
+
<%= form_for @user do |f| %>
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
<div class="user-edit-item">
|
550
|
+
|
551
|
+
<%= f.label :name, "ユーザー名(※必須)", class: "form" %><br>
|
552
|
+
|
553
|
+
<%= f.text_field :name %><br>
|
554
|
+
|
555
|
+
|
556
|
+
|
557
|
+
<%= f.label :image, "画像", class: "form" %><br>
|
558
|
+
|
559
|
+
<%= f.file_field :image %><br>
|
560
|
+
|
561
|
+
|
562
|
+
|
563
|
+
<%= f.label :email, "メールアドレス(※必須)", class: "form" %><br>
|
564
|
+
|
565
|
+
<%= f.text_field :email %><br>
|
566
|
+
|
567
|
+
|
568
|
+
|
569
|
+
<%= f.label :password, "パスワード(※必須)", class: "form" %><br>
|
570
|
+
|
571
|
+
<%= f.text_field :password %><br>
|
572
|
+
|
573
|
+
|
574
|
+
|
575
|
+
<%= f.submit "新規登録", class: "form" %>
|
576
|
+
|
577
|
+
|
578
|
+
|
579
|
+
</div>
|
580
|
+
|
581
|
+
<% end %>
|
582
|
+
|
583
|
+
</div>
|
584
|
+
|
585
|
+
</div>
|
586
|
+
|
587
|
+
|
588
|
+
|
589
|
+
```
|
590
|
+
|
591
|
+
|
592
|
+
|
519
593
|
|
520
594
|
|
521
595
|
|
4
説明追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -456,14 +456,14 @@
|
|
456
456
|
|
457
457
|
t.string "email"
|
458
458
|
|
459
|
-
t.string "password"
|
460
|
-
|
461
459
|
t.string "image"
|
462
460
|
|
463
461
|
t.datetime "created_at", precision: 6, null: false
|
464
462
|
|
465
463
|
t.datetime "updated_at", precision: 6, null: false
|
466
464
|
|
465
|
+
t.string "password_digest"
|
466
|
+
|
467
467
|
end
|
468
468
|
|
469
469
|
|
3
説明追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -208,25 +208,75 @@
|
|
208
208
|
|
209
209
|
def login
|
210
210
|
|
211
|
-
@user = User.find_by(email: params[:email]
|
211
|
+
@user = User.find_by(email: params[:email])
|
212
|
-
|
212
|
+
|
213
|
+
|
214
|
+
|
213
|
-
if @user
|
215
|
+
if @user && @user.authenticate(params[:password])
|
214
|
-
|
216
|
+
|
215
|
-
session[:user_id] = @user.id
|
217
|
+
session[:user_id] = @user.id
|
216
|
-
|
218
|
+
|
217
|
-
flash[:notice] = "ログイン成功"
|
219
|
+
flash[:notice] = "ログイン成功"
|
218
|
-
|
220
|
+
|
219
|
-
redirect_to("/posts/index")
|
221
|
+
redirect_to("/posts/index")
|
222
|
+
|
223
|
+
else
|
224
|
+
|
225
|
+
@error_message = "メールアドレスかパスワードが違います"
|
226
|
+
|
227
|
+
@email = params[:email]
|
228
|
+
|
229
|
+
@password = params[:password]
|
230
|
+
|
231
|
+
render("users/login_form")
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
def logout
|
242
|
+
|
243
|
+
session[:user_id] = nil
|
244
|
+
|
245
|
+
flash[:notice] = "ログアウトしました"
|
246
|
+
|
247
|
+
redirect_to("/login")
|
248
|
+
|
249
|
+
end
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
def edit
|
256
|
+
|
257
|
+
@user = User.find_by(id: params[:id])
|
258
|
+
|
259
|
+
end
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
def update
|
266
|
+
|
267
|
+
@user = User.find_by(id: params[:id])
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
if @user.update(user_params)
|
272
|
+
|
273
|
+
flash[:notice] = "ユーザー情報を変更しました"
|
274
|
+
|
275
|
+
redirect_to("/users/#{@user.id}")
|
220
276
|
|
221
277
|
else
|
222
278
|
|
223
|
-
@error_message = "メールアドレスかパスワードが違います"
|
224
|
-
|
225
|
-
@email = params[:email]
|
226
|
-
|
227
|
-
@password = params[:password]
|
228
|
-
|
229
|
-
render("users/
|
279
|
+
render("/users/edit")
|
230
280
|
|
231
281
|
end
|
232
282
|
|
@@ -236,82 +286,34 @@
|
|
236
286
|
|
237
287
|
|
238
288
|
|
239
|
-
def logout
|
240
|
-
|
241
|
-
session[:user_id] = nil
|
242
|
-
|
243
|
-
flash[:notice] = "ログアウトしました"
|
244
|
-
|
245
|
-
redirect_to("/login")
|
246
|
-
|
247
|
-
end
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
def e
|
289
|
+
def destroy
|
254
|
-
|
255
|
-
@user = User.find_by(id: params[:id])
|
256
|
-
|
257
|
-
end
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
def update
|
264
290
|
|
265
291
|
@user = User.find_by(id: params[:id])
|
266
292
|
|
267
|
-
|
268
|
-
|
269
|
-
|
293
|
+
@user.destroy
|
270
|
-
|
271
|
-
|
294
|
+
|
272
|
-
|
273
|
-
|
295
|
+
redirect_to("/users/index")
|
296
|
+
|
274
|
-
|
297
|
+
flash[:notice] = "削除しました"
|
298
|
+
|
275
|
-
|
299
|
+
end
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
|
308
|
+
|
276
|
-
|
309
|
+
private
|
310
|
+
|
311
|
+
def user_params
|
312
|
+
|
277
|
-
re
|
313
|
+
params.require(:user).permit(:name, :email, :password, :image)
|
278
314
|
|
279
315
|
end
|
280
316
|
|
281
|
-
end
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
def destroy
|
288
|
-
|
289
|
-
@user = User.find_by(id: params[:id])
|
290
|
-
|
291
|
-
@user.destroy
|
292
|
-
|
293
|
-
redirect_to("/users/index")
|
294
|
-
|
295
|
-
flash[:notice] = "削除しました"
|
296
|
-
|
297
|
-
end
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
private
|
308
|
-
|
309
|
-
def user_params
|
310
|
-
|
311
|
-
params.require(:user).permit(:name, :email, :password, :image)
|
312
|
-
|
313
|
-
end
|
314
|
-
|
315
317
|
|
316
318
|
|
317
319
|
|
@@ -474,6 +476,46 @@
|
|
474
476
|
|
475
477
|
|
476
478
|
|
479
|
+
```
|
480
|
+
|
481
|
+
models/User
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
class User < ApplicationRecord
|
486
|
+
|
487
|
+
has_secure_password
|
488
|
+
|
489
|
+
mount_uploader :image,ImageUploader
|
490
|
+
|
491
|
+
validates :name, {presence: true}
|
492
|
+
|
493
|
+
validates :email, {presence: true}
|
494
|
+
|
495
|
+
validates :password, {presence: true}
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
|
500
|
+
|
501
|
+
def posts
|
502
|
+
|
503
|
+
return Post.where(user_id: self.id)
|
504
|
+
|
505
|
+
end
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
paginates_per 8
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
end
|
514
|
+
|
515
|
+
```
|
516
|
+
|
517
|
+
|
518
|
+
|
477
519
|
|
478
520
|
|
479
521
|
|
2
説明追加
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
ログインフォームをform_forで作成できず、困っています
|
test
CHANGED
File without changes
|
1
説明追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -480,6 +480,24 @@
|
|
480
480
|
|
481
481
|
自分で試したこととしては、form_forの@userがnilなので、login_formアクション内で@userオブジェクトを生成したりもしたのですが、特に変わらず、、
|
482
482
|
|
483
|
-
|
483
|
+
(具体的には、
|
484
|
+
|
484
|
-
|
485
|
+
・@user = User.find_by(email: params[:email], password: params[:password])
|
486
|
+
|
487
|
+
・@user = User.find_by(id: params[:id])
|
488
|
+
|
489
|
+
など。)
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
sessionコントローラーなどは用いずに実装したいと考えています。(できないのであれば仕方ないとは考えています)。
|
494
|
+
|
495
|
+
また、form_tagで実装すると機能するので、form_tagのままでいいかなとも思いつつ、全体としてform_forで揃えるべきだよな、、とも考えています。
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
|
500
|
+
|
501
|
+
|
502
|
+
|
485
|
-
もしお分かりの方がいらっしゃいましたら、アドバイスいただけると幸いです。
|
503
|
+
login_formアクション内で@userオブジェクトに何を定義すればいいのでしょうか?もしお分かりの方がいらっしゃいましたら、アドバイスいただけると幸いです。
|