質問編集履歴
2
application_controller.rbの追加。
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,6 +8,7 @@
|
|
8
8
|
ちなみにログイン機能を実装した際はgem 'devise'は使用していません。
|
9
9
|
そのためかcurrent_userが使用できないのかと考えてはいましたが他の箇所でもcurrent_userを
|
10
10
|
使用していました。
|
11
|
+
application_controller.rbではdef current_userで定義してあります。
|
11
12
|
またrspecにてbeforeでログインしていないという動作を怠ったということも考えましたが
|
12
13
|
ちゃんとログインするような記述はしておりました。
|
13
14
|
どなたか原因特定できる方もしいましたらご教授お願い致します。
|
@@ -293,6 +294,31 @@
|
|
293
294
|
end
|
294
295
|
```
|
295
296
|
|
297
|
+
```
|
298
|
+
class ApplicationController < ActionController::Base
|
299
|
+
helper_method :current_user
|
300
|
+
|
301
|
+
def login?
|
302
|
+
if current_user.nil?
|
303
|
+
redirect_to login_path, alert: "ログインが必要です。"
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
def already_login?
|
308
|
+
unless current_user.nil?
|
309
|
+
redirect_to user_path, notice: "ログイン済みです。"
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
def current_user
|
314
|
+
if session[:user_id]
|
315
|
+
current_user ||= User.find(session[:user_id])
|
316
|
+
end
|
317
|
+
current_user
|
318
|
+
end
|
319
|
+
end
|
320
|
+
```
|
321
|
+
|
296
322
|
### 試したこと
|
297
323
|
|
298
324
|
controllerのshowの箇所に@user = current_user.idの記載をしてみましたが
|
1
コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,6 +8,8 @@
|
|
8
8
|
ちなみにログイン機能を実装した際はgem 'devise'は使用していません。
|
9
9
|
そのためかcurrent_userが使用できないのかと考えてはいましたが他の箇所でもcurrent_userを
|
10
10
|
使用していました。
|
11
|
+
またrspecにてbeforeでログインしていないという動作を怠ったということも考えましたが
|
12
|
+
ちゃんとログインするような記述はしておりました。
|
11
13
|
どなたか原因特定できる方もしいましたらご教授お願い致します。
|
12
14
|
|
13
15
|
### 発生している問題・エラーメッセージ
|
@@ -207,6 +209,90 @@
|
|
207
209
|
end
|
208
210
|
```
|
209
211
|
|
212
|
+
```
|
213
|
+
require 'rails_helper'
|
214
|
+
|
215
|
+
RSpec.describe "Caves", type: :request do
|
216
|
+
describe "#new" do
|
217
|
+
let(:cafe) { create(:cafe) }
|
218
|
+
let(:user) { create(:user) }
|
219
|
+
|
220
|
+
before do
|
221
|
+
session_params = { session: { email: user.email, password: user.password } }
|
222
|
+
post "/login", params: session_params
|
223
|
+
end
|
224
|
+
|
225
|
+
it 'カフェ登録画面の表示に成功すること' do
|
226
|
+
get '/caves/new'
|
227
|
+
expect(response).to have_http_status(200)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
describe "#create" do
|
232
|
+
let(:cafe) { create(:cafe) }
|
233
|
+
let(:user) { create(:user) }
|
234
|
+
|
235
|
+
before do
|
236
|
+
session_params = { session: { email: user.email, password: user.password } }
|
237
|
+
post "/login", params: session_params
|
238
|
+
end
|
239
|
+
|
240
|
+
it 'カフェ作成に成功すること' do
|
241
|
+
valid_params = { image: cafe.image, name: cafe.name, address: cafe.address, number_seats: cafe.number_seats }
|
242
|
+
expect { post '/caves/new', params: { cafe: valid_params } }
|
243
|
+
expect(response).to have_http_status(200)
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
describe "#show" do
|
248
|
+
let(:cafe) { create(:cafe) }
|
249
|
+
let(:user) { create(:user) }
|
250
|
+
|
251
|
+
before do
|
252
|
+
session_params = { session: { email: user.email, password: user.password } }
|
253
|
+
post "/login", params: session_params
|
254
|
+
end
|
255
|
+
|
256
|
+
it 'カフェ詳細画面の表示に成功すること' do
|
257
|
+
get "/caves/#{cafe.id}"
|
258
|
+
expect(response).to have_http_status(200)
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
describe "#edit" do
|
263
|
+
let(:cafe) { create(:cafe) }
|
264
|
+
let(:user) { create(:user) }
|
265
|
+
|
266
|
+
before do
|
267
|
+
session_params = { session: { email: user.email, password: user.password } }
|
268
|
+
post "/login", params: session_params
|
269
|
+
end
|
270
|
+
|
271
|
+
it 'カフェ編集画面の表示に成功すること' do
|
272
|
+
get "/caves/#{cafe.id}/edit"
|
273
|
+
expect(response).to have_http_status(200)
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
describe "#update" do
|
278
|
+
let(:cafe) { create(:cafe) }
|
279
|
+
let(:user) { create(:user) }
|
280
|
+
|
281
|
+
before do
|
282
|
+
session_params = { session: { email: user.email, password: user.password } }
|
283
|
+
post "/login", params: session_params
|
284
|
+
end
|
285
|
+
|
286
|
+
it 'カフェの編集に成功すること' do
|
287
|
+
get "/caves/#{cafe.id}/edit"
|
288
|
+
valid_params = { image: cafe.image, name: cafe.name, address: cafe.address, number_seats: cafe.number_seats }
|
289
|
+
expect { put "/caves/#{cafe.id}", params: { cafe: valid_params } }
|
290
|
+
expect(response).to have_http_status(200)
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
294
|
+
```
|
295
|
+
|
210
296
|
### 試したこと
|
211
297
|
|
212
298
|
controllerのshowの箇所に@user = current_user.idの記載をしてみましたが
|