LoginSignup
8
15

More than 5 years have passed since last update.

link_toでshow(:id)へのリンクを生成

Last updated at Posted at 2018-10-11

link_toで/:idのリンクを作る

URLが/user/:idの詳細ページへのリンクを作りたい

controllerでユーザーのIDを取得

user_controller.rb
def show
 @user = User.find(current_user.id)
end

IDは@userに入る

user.html.erb
<% link_to('Show', user_path(@user)) %>
user.html.erb
<% link_to('Show', user_path(current_user.id)) %>

とも書くことができた。

8
15
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
8
15