LoginSignup
0
0

More than 3 years have passed since last update.

users_controllerでbinding.pryを使用した時になぜユーザーidが取得できているのか

Last updated at Posted at 2020-03-07

users_controllerでbinding.pryを使用した時

users_controller.rb
def show
    binding.pry
    user = User.find(params[:id])
    @nickname = user.nickname
    @tweets = user.tweets.page(params[:page]).per(5).order("created_at DESC")

【質問】
binding.pryを入力し、ターミナルにparamsを入力するとしっかりユーザーidが取れて来ている。
user = User.find(params[:id])より前にbinding.pryを使用しているのに
なぜユーザーidが取れているの?という質問に対して即答できませんでした。。。

【答え】
前提としてログインしている時点でユーザーidの情報は持っているし、
マイページに飛ぶ際にリクエストで、current_user.idを記載しURLと一緒に飛ばしている。

application.html.erb
<a href="/users/<%= current_user.id %>">マイページ</a>

つまり、ユーザーidの情報をusers.controllerでのbinding.pry以前から
持っているので、ターミナル上のparamsでユーザーidを取得されている事が確認できる。

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