0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

current_userの値はどこで入れられてるのでしょう?

Posted at

初めに

疑問に思ったことや上手くいかなかったことのアウトプットをしています。
自分なりの理解でアウトプットしていきます。初学者なので誤りもあると思います。
その際はご指摘いただけると幸いです。

疑問を持ったきっかけ

users_controllerで

users_controller.rb
def correct_user?
    @user == current_user
end

みたいに、当たり前のようにcurrent_userと書いてあるけど、いったいどこで定義されているのだろう?と思ったのがきっかけです。
user_sessions_controllerでおそらく入ったのだと思いますが、どこにもcurrent_userと書いてありません。

調べると

直接的には目に見えませんが、Railsではloginメソッドが成功すると、内部でセッションにユーザー情報が保存される仕組みになっているので、その時に格納されます。
以降のリクエストでcurrent_userを呼び出すと、ログイン中のユーザー情報を簡単に参照できるようになります。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?