こんにちは、ProgateでRailsの学習をしています。
一覧ページから詳細ページに飛ぶリンクを作るところで躓きました。
なぜ、以下のようになるのでしょうか?
× <%= link_to(#{user.name},"/users/#{user.id}") %>
○ <%= link_to(user.name,"/users/#{user.id}") %>
link_toで囲う、前半です。
userは変数ですよね?
変数なのに、#{}で囲まないのは不思議です。
慣習でしょうか?
index.html.erb
1<div class="main users-index"> 2 <div class="container"> 3 <h1 class="users-heading">ユーザー一覧</h1> 4 <% @users.each do |user| %> 5 <div class="users-index-item"> 6 <div class="user-right"> 7 <!-- 以下のユーザー名を、ユーザー詳細ページへのリンクに書き換えてください --> 8 <%= link_to(user.name,"/users/#{user.id}") %> 9 </div> 10 </div> 11 <% end %> 12 </div> 13</div> 14
users.controller.rb
1class UsersController < ApplicationController 2 def index 3 @users = User.all 4 end 5 6 # showアクションを追加してください 7 def show 8 @user = User.find_by(id: params[:id] ) 9 end 10 11 12end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/12/18 04:02 編集