質問編集履歴
3
内容の追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
画像のようなエラーになっていまします。
|
4
4
|
解決方法が全くわかりません。
|
5
5
|
|
6
|
-
app/helpers/sessions_helper.rbのコードです。
|
6
|
+
#app/helpers/sessions_helper.rbのコードです。
|
7
7
|
module SessionsHelper
|
8
8
|
|
9
9
|
渡されたユーザーでログインする
|
@@ -47,7 +47,7 @@
|
|
47
47
|
@current_user = nil
|
48
48
|
end
|
49
49
|
|
50
|
-
|
50
|
+
永続的セッションを破棄する
|
51
51
|
def forget(user)
|
52
52
|
user.forget
|
53
53
|
cookies.delete(:user_id)
|
@@ -76,7 +76,7 @@
|
|
76
76
|
|
77
77
|
|
78
78
|
|
79
|
-
app/controllers/users.controller.rbのコードです。
|
79
|
+
# app/controllers/users.controller.rbのコードです。
|
80
80
|
|
81
81
|
class UsersController < ApplicationController
|
82
82
|
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
|
2
タグの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -6,19 +6,19 @@
|
|
6
6
|
app/helpers/sessions_helper.rbのコードです。
|
7
7
|
module SessionsHelper
|
8
8
|
|
9
|
-
|
9
|
+
渡されたユーザーでログインする
|
10
10
|
def log_in(user)
|
11
11
|
session[:user_id] = user.id
|
12
12
|
end
|
13
13
|
|
14
|
-
|
14
|
+
ユーザーのセッションを永続的にする
|
15
15
|
def remember(user)
|
16
16
|
user.remember
|
17
17
|
cookies.permanent.signed[:user_id] = user.id
|
18
18
|
cookies.permanent[:remember_token] = user.remember_token
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
記憶トークンcookieに対応するユーザーを返す
|
22
22
|
def current_user
|
23
23
|
if (user_id = session[:user_id])
|
24
24
|
current_user ||= User.find_by(id: user_id)
|
@@ -31,48 +31,51 @@
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
|
34
|
+
渡されたユーザーがカレントユーザーであればtrueを返す
|
35
35
|
def current_user?(user)
|
36
36
|
user && user == current_user
|
37
37
|
end
|
38
38
|
|
39
|
-
|
39
|
+
ユーザーがログインしていればtrue、その他ならfalseを返す
|
40
40
|
def logged_in?
|
41
41
|
!current_user.nil?
|
42
42
|
end
|
43
43
|
|
44
|
-
|
44
|
+
現在のユーザーをログアウトする
|
45
45
|
def log_out
|
46
46
|
session.delete(:user_id)
|
47
47
|
@current_user = nil
|
48
48
|
end
|
49
49
|
|
50
|
-
#
|
50
|
+
#永続的セッションを破棄する
|
51
51
|
def forget(user)
|
52
52
|
user.forget
|
53
53
|
cookies.delete(:user_id)
|
54
54
|
cookies.delete(:remember_token)
|
55
55
|
end
|
56
56
|
|
57
|
-
|
57
|
+
現在のユーザーをログアウトする
|
58
58
|
def log_out
|
59
59
|
forget(current_user)
|
60
60
|
session.delete(:user_id)
|
61
61
|
@current_user = nil
|
62
62
|
end
|
63
63
|
|
64
|
-
|
64
|
+
記憶したURL (もしくはデフォルト値) にリダイレクト
|
65
65
|
def redirect_back_or(default)
|
66
66
|
redirect_to(session[:forwarding_url] || default)
|
67
67
|
session.delete(:forwarding_url)
|
68
68
|
end
|
69
69
|
|
70
|
-
|
70
|
+
アクセスしようとしたURLを覚えておく
|
71
71
|
def store_location
|
72
72
|
session[:forwarding_url] = request.original_url if request.get?
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
+
|
77
|
+
|
78
|
+
|
76
79
|
app/controllers/users.controller.rbのコードです。
|
77
80
|
|
78
81
|
class UsersController < ApplicationController
|
@@ -130,9 +133,9 @@
|
|
130
133
|
:password_confirmation)
|
131
134
|
end
|
132
135
|
|
133
|
-
|
136
|
+
beforeアクション
|
134
137
|
|
135
|
-
|
138
|
+
ログイン済みユーザーかどうか確認
|
136
139
|
def logged_in_user
|
137
140
|
unless logged_in?
|
138
141
|
store_location
|
@@ -141,13 +144,13 @@
|
|
141
144
|
end
|
142
145
|
end
|
143
146
|
|
144
|
-
|
147
|
+
正しいユーザーかどうか確認
|
145
148
|
def correct_user
|
146
149
|
@user = User.find(params[:id])
|
147
150
|
redirect_to(root_url) unless current_user?(@user)
|
148
151
|
end
|
149
152
|
|
150
|
-
|
153
|
+
管理者かどうか確認
|
151
154
|
def admin_user
|
152
155
|
redirect_to(root_url) unless current_user.admin?
|
153
156
|
end
|
1
内容の追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,154 @@
|
|
1
1
|

|
2
2
|
|
3
3
|
画像のようなエラーになっていまします。
|
4
|
-
解決方法が全くわかりません。
|
4
|
+
解決方法が全くわかりません。
|
5
|
+
|
6
|
+
app/helpers/sessions_helper.rbのコードです。
|
7
|
+
module SessionsHelper
|
8
|
+
|
9
|
+
# 渡されたユーザーでログインする
|
10
|
+
def log_in(user)
|
11
|
+
session[:user_id] = user.id
|
12
|
+
end
|
13
|
+
|
14
|
+
# ユーザーのセッションを永続的にする
|
15
|
+
def remember(user)
|
16
|
+
user.remember
|
17
|
+
cookies.permanent.signed[:user_id] = user.id
|
18
|
+
cookies.permanent[:remember_token] = user.remember_token
|
19
|
+
end
|
20
|
+
|
21
|
+
# 記憶トークンcookieに対応するユーザーを返す
|
22
|
+
def current_user
|
23
|
+
if (user_id = session[:user_id])
|
24
|
+
current_user ||= User.find_by(id: user_id)
|
25
|
+
elsif (user_id = cookies.signed[:user_id])
|
26
|
+
user = User.find_by(id: user_id)
|
27
|
+
if user && user.authenticated?(:remember, cookies[:remember_token])
|
28
|
+
log_in user
|
29
|
+
@current_user = user
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# 渡されたユーザーがカレントユーザーであればtrueを返す
|
35
|
+
def current_user?(user)
|
36
|
+
user && user == current_user
|
37
|
+
end
|
38
|
+
|
39
|
+
# ユーザーがログインしていればtrue、その他ならfalseを返す
|
40
|
+
def logged_in?
|
41
|
+
!current_user.nil?
|
42
|
+
end
|
43
|
+
|
44
|
+
# 現在のユーザーをログアウトする
|
45
|
+
def log_out
|
46
|
+
session.delete(:user_id)
|
47
|
+
@current_user = nil
|
48
|
+
end
|
49
|
+
|
50
|
+
# 永続的セッションを破棄する
|
51
|
+
def forget(user)
|
52
|
+
user.forget
|
53
|
+
cookies.delete(:user_id)
|
54
|
+
cookies.delete(:remember_token)
|
55
|
+
end
|
56
|
+
|
57
|
+
# 現在のユーザーをログアウトする
|
58
|
+
def log_out
|
59
|
+
forget(current_user)
|
60
|
+
session.delete(:user_id)
|
61
|
+
@current_user = nil
|
62
|
+
end
|
63
|
+
|
64
|
+
# 記憶したURL (もしくはデフォルト値) にリダイレクト
|
65
|
+
def redirect_back_or(default)
|
66
|
+
redirect_to(session[:forwarding_url] || default)
|
67
|
+
session.delete(:forwarding_url)
|
68
|
+
end
|
69
|
+
|
70
|
+
# アクセスしようとしたURLを覚えておく
|
71
|
+
def store_location
|
72
|
+
session[:forwarding_url] = request.original_url if request.get?
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
app/controllers/users.controller.rbのコードです。
|
77
|
+
|
78
|
+
class UsersController < ApplicationController
|
79
|
+
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
|
80
|
+
before_action :correct_user, only: [:edit, :update]
|
81
|
+
before_action :admin_user, only: :destroy
|
82
|
+
|
83
|
+
def index
|
84
|
+
@users = User.paginate(page: params[:page])
|
85
|
+
end
|
86
|
+
|
87
|
+
def show
|
88
|
+
@user = User.find(params[:id])
|
89
|
+
end
|
90
|
+
|
91
|
+
def new
|
92
|
+
@user = User.new
|
93
|
+
end
|
94
|
+
|
95
|
+
def create
|
96
|
+
@user = User.new(user_params)
|
97
|
+
if @user.save
|
98
|
+
@user.send_activation_email
|
99
|
+
flash[:info] = "Please check your email to activate your account."
|
100
|
+
redirect_to root_url
|
101
|
+
else
|
102
|
+
render 'new'
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def edit
|
107
|
+
@user = User.find(params[:id])
|
108
|
+
end
|
109
|
+
|
110
|
+
def update
|
111
|
+
@user = User.find(params[:id])
|
112
|
+
if @user.update(user_params)
|
113
|
+
flash[:success] = "Profile updated"
|
114
|
+
redirect_to @user
|
115
|
+
else
|
116
|
+
render 'edit'
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def destroy
|
121
|
+
User.find(params[:id]).destroy
|
122
|
+
flash[:success] = "User deleted"
|
123
|
+
redirect_to users_url
|
124
|
+
end
|
125
|
+
|
126
|
+
private
|
127
|
+
|
128
|
+
def user_params
|
129
|
+
params.require(:user).permit(:name, :email, :password,
|
130
|
+
:password_confirmation)
|
131
|
+
end
|
132
|
+
|
133
|
+
# beforeアクション
|
134
|
+
|
135
|
+
# ログイン済みユーザーかどうか確認
|
136
|
+
def logged_in_user
|
137
|
+
unless logged_in?
|
138
|
+
store_location
|
139
|
+
flash[:danger] = "Please log in."
|
140
|
+
redirect_to login_url
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# 正しいユーザーかどうか確認
|
145
|
+
def correct_user
|
146
|
+
@user = User.find(params[:id])
|
147
|
+
redirect_to(root_url) unless current_user?(@user)
|
148
|
+
end
|
149
|
+
|
150
|
+
# 管理者かどうか確認
|
151
|
+
def admin_user
|
152
|
+
redirect_to(root_url) unless current_user.admin?
|
153
|
+
end
|
154
|
+
end
|