質問編集履歴

2

logged_inのコードを追加

2021/03/31 23:25

投稿

pecchan
pecchan

スコア555

test CHANGED
File without changes
test CHANGED
@@ -149,3 +149,135 @@
149
149
  end
150
150
 
151
151
  ```
152
+
153
+
154
+
155
+
156
+
157
+ ### 追記
158
+
159
+ logged_inは、SessionHelperから呼び出してます。
160
+
161
+ Railsチュートリアルを流用しました。
162
+
163
+ ```ruby
164
+
165
+ module SessionsHelper
166
+
167
+
168
+
169
+ # 渡されたユーザーでログインする
170
+
171
+ def log_in(user)
172
+
173
+ session[:user_id] = user.id
174
+
175
+ end
176
+
177
+
178
+
179
+ # ユーザーのセッションを永続的にする
180
+
181
+ def remember(user)
182
+
183
+ user.remember
184
+
185
+ cookies.permanent.signed[:user_id] = user.id
186
+
187
+ cookies.permanent[:remember_token] = user.remember_token
188
+
189
+ end
190
+
191
+
192
+
193
+ # 現在ログイン中のユーザーを返す (いる場合)
194
+
195
+ def current_user
196
+
197
+ if (user_id = session[:user_id])
198
+
199
+ @current_user ||= User.find_by(id: user_id)
200
+
201
+ elsif (user_id = cookies.signed[:user_id])
202
+
203
+ user = User.find_by(id: user_id)
204
+
205
+ if user && user.authenticated?(:remember, cookies[:remember_token])
206
+
207
+ log_in(user)
208
+
209
+ @current_user = user
210
+
211
+ end
212
+
213
+ end
214
+
215
+ end
216
+
217
+
218
+
219
+ # ユーザーがログインしていればtrue、その他ならfalseを返す
220
+
221
+ def logged_in?
222
+
223
+ !current_user.nil?
224
+
225
+ end
226
+
227
+
228
+
229
+ # 永続的セッションを破棄する
230
+
231
+ def forget(user)
232
+
233
+ user.forget
234
+
235
+ cookies.delete(:user_id)
236
+
237
+ cookies.delete(:remember_token)
238
+
239
+ end
240
+
241
+
242
+
243
+ # 現在のユーザーをログアウトする
244
+
245
+ def log_out
246
+
247
+ forget(current_user)
248
+
249
+ session.delete(:user_id)
250
+
251
+ @current_user = nil
252
+
253
+ end
254
+
255
+
256
+
257
+ # 記憶したURL (もしくはデフォルト値) にリダイレクト
258
+
259
+ def redirect_back_or(default)
260
+
261
+ redirect_to(session[:forwarding_url] || default)
262
+
263
+ session.delete(:forwarding_url)
264
+
265
+ end
266
+
267
+
268
+
269
+ # アクセスしようとしたURLを覚えておく
270
+
271
+ def store_location
272
+
273
+ session[:forwarding_url] = request.original_url if request.get?
274
+
275
+ end
276
+
277
+
278
+
279
+ end
280
+
281
+
282
+
283
+ ```

1

ログイン確認してたコードを追加

2021/03/31 23:25

投稿

pecchan
pecchan

スコア555

test CHANGED
File without changes
test CHANGED
@@ -24,7 +24,11 @@
24
24
 
25
25
  app\controllers\item_questions_controller.rb
26
26
 
27
- ```ruty
27
+ ```ruby
28
+
29
+ class ItemQuestionsController < LoginController
30
+
31
+ #before_action :logged_in_user, only: [:new, :create, :update]
28
32
 
29
33
  def update
30
34
 
@@ -33,6 +37,8 @@
33
37
  @question.update(question_params)
34
38
 
35
39
  end
40
+
41
+ end
36
42
 
37
43
  ```
38
44
 
@@ -91,3 +97,55 @@
91
97
  分かる方教えていただけないでしょうか?
92
98
 
93
99
  宜しくお願いします。
100
+
101
+
102
+
103
+ ### 追記
104
+
105
+ controllerの一行目、
106
+
107
+ before_action :logged_in_user
108
+
109
+ をコメントアウトすると、通りました。
110
+
111
+
112
+
113
+ このlogged_in_user()の目的は、ログインしてないと登録、更新はさせないという意図です。
114
+
115
+ コードは、このようになってます。
116
+
117
+ app\controllers\concerns\login_controller.rb
118
+
119
+ ```ruby
120
+
121
+ class LoginController < ApplicationController
122
+
123
+
124
+
125
+ private
126
+
127
+ # beforeアクション ログイン済みユーザーかどうか確認
128
+
129
+ def logged_in_user
130
+
131
+ unless logged_in?
132
+
133
+ store_location
134
+
135
+ flash[:danger] = "ログインしてください"
136
+
137
+ redirect_to login_url
138
+
139
+ end
140
+
141
+ end
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+ end
150
+
151
+ ```