LoginSignup
6
5

More than 3 years have passed since last update.

【Ruby on Rails】undefined method `id' for nil:NilClassのエラー解消方法

Last updated at Posted at 2020-05-20

"undefined method `id' for nil:NilClass"のエラー

スクリーンショット 2020-05-20 19.33.13.png

もし打ち間違えなどがないのにこのようなNoMethodErrorのundefined method `id' for nil:NilClassのエラーというエラーがでた場合どんなことが原因として考えられるか。メソッドが定義されていないというエラーがでました...どうしてきちんと定義したのにこのようなエラーがでたのだろう...

この実装はログインしたユーザーには自分の情報が見れても、他のユーザーには見せたくない場合にこのような実装を用います。
<% if @user.id == current_user.id %>

実装したコードの記述に間違いがないのにどうしてこうなったんだろうと考えてあることに気がつきます!

ログインをしていませんでした!

ログインをしていないと当然成立しない処理なので当然ですよね^_^;
ログインしてから再度確認をしたら無事にエラーが解消されました!

S__56467458.jpg

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5