LoginSignup
0
0

More than 3 years have passed since last update.

ユーザー名の表示

Posted at

sessionを使ってユーザー名の表示をさせる方法
前提条件
1.userテーブルがあること。DBのこと

session[:user_id]の値をもとにしてログイン中のユーザー情報をDB(データベース)から取得する。
その際には、find_byメソッドを使い、userテーブルからidカラムの値がsession[:user_id]と同じユーザーを取得して、変数に代入する。

つまり?
*DBに登録されているユーザーのidを探してsession[:user_id]と同じであれば代入される形

【使い方】
layouts/application.html.erb

<% cureent_user = User.find_by(id:session[:user_id]) %>


<% = link_to(cureent_user.name, "/users/#{current_user.id}") %>

*cureent_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