質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

3358閲覧

ユーザー名をクリックすると、ユーザー詳細ページが表示されるようにしたい

sydds

総合スコア2

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

1クリップ

投稿2021/04/12 04:26

ユーザー名をクリックすると、ユーザー詳細ページが表示されるようにしたいのですが
うまく表示されません。
おそらくusersのusersのshow.html.eabをどうにかするのだと思うのですが…。
現在、ログインしたユーザー名をクリックするとviews/users/showの見本の詳細が表示されます。
ユーザー登録した時の情報を表示させるにはどうしたら良いのかご教示頂けると幸いです。

routes.rb

ruby

1Rails.application.routes.draw do 2 devise_for :users 3 get 'prototypes/index' 4 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 5 root to: "prototypes#index" 6 resources :prototypes, only: [:index, :new, :create, :show, :edit, :update, :destroy] do 7 resources :comments, only: [:create] 8 end 9 resources :users, only: [:show] 10end

usersコントローラー

ruby

1class UsersController < ApplicationController 2 3def show 4 @user = User.find(params[:id]) 5end 6 7end

prototypesコントローラー

ruby

1class PrototypesController < ApplicationController 2 def index 3 @prototype = Prototype.all 4 end 5 6 def new 7 @prototype = Prototype.new 8 end 9 10 def create 11 @prototype = Prototype.new(prototype_params) 12 if @prototype.save 13 redirect_to root_path 14 else 15 render :new 16 end 17 end 18 19 def show 20 @prototype = Prototype.find(params[:id]) 21 @comment = Comment.new 22 @comments = @prototype.comments.includes(:user) 23 end 24 25 def edit 26 @prototype = Prototype.find(params[:id]) 27 end 28 29 def update 30 @prototype = Prototype.find(params[:id]) 31 if @prototype.update(prototype_params) 32 redirect_to action: "show" 33 else 34 render :edit 35 end 36 end 37 38 def destroy 39 prototype = Prototype.find(params[:id]) 40 prototype.destroy 41 redirect_to action: "index" 42 end 43 44 private 45 46 def prototype_params 47 params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) 48 end 49 50end

views/prototypes/index.html.erb

ruby

1<main class="main"> 2 <div class="inner"> 3 <%# ログインしているときは以下を表示する %> 4 <% if user_signed_in? %> 5 <div class="greeting"> 6 <%= "こんにちは、" %> 7 <%= link_to current_user.name, user_path(id: current_user) %> 8 <%= "さん" %> 9 </div> 10 <% end %> 11 <%# // ログインしているときは上記を表示する %> 12 <div class="card__wrapper"> 13 <%# 投稿機能実装後、部分テンプレートでプロトタイプ投稿一覧を表示する %> 14 <%= render partial: "prototype", collection: @prototype %> 15 </div> 16 </div> 17</main>

views/prototypes/show.html.erb

ruby

1<main class="main"> 2 <div class="inner"> 3 <div class="prototype__wrapper"> 4 <p class="prototype__hedding"> 5 <%= @prototype.title %> 6 </p> 7 <%= link_to "by #{@prototype.user.name}", root_path, class: :prototype__user %> 8 <%# プロトタイプの投稿者とログインしているユーザーが同じであれば以下を表示する %> 9 <% if user_signed_in? && current_user.id == @prototype.user_id %> 10 <div class="prototype__manage"> 11 <%= link_to "編集する", edit_prototype_path(@prototype.id), class: :prototype__btn %> 12 <%= link_to "削除する", prototype_path(@prototype.id), method: :delete, class: :prototype__btn %> 13 </div> 14 <% end %> 15 <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> 16 <div class="prototype__image"> 17 <%= image_tag @prototype.image, class: :card__img %> 18 </div> 19 <div class="prototype__body"> 20 <div class="prototype__detail"> 21 <p class="detail__title">キャッチコピー</p> 22 <p class="detail__message"> 23 <%= @prototype.catch_copy %> 24 </p> 25 </div> 26 <div class="prototype__detail"> 27 <p class="detail__title">コンセプト</p> 28 <p class="detail__message"> 29 <%= @prototype.concept %> 30 </p> 31 </div> 32 </div> 33 <div class="prototype__comments"> 34 <%# ログインしているユーザーには以下のコメント投稿フォームを表示する %> 35 <% if user_signed_in? %> 36 <%= form_with model: [@prototype, @comment], local: true do |f|%> 37 <div class="field"> 38 <%= f.label :text, "コメント" %><br /> 39 <%= f.text_field :text, id:"comment_text" %> 40 </div> 41 <div class="actions"> 42 <%= f.submit "送信する", class: :form__btn %> 43 </div> 44 <% end %> 45 <% end %> 46 <%# // ログインしているユーザーには上記を表示する %> 47 <ul class="comments_lists"> 48 <%# 投稿に紐づくコメントを一覧する処理を記述する %> 49 <% @comments.each do |comment| %> 50 <li class="comments_list"> 51 <%= comment.text %> 52 <%= link_to @prototype.user.name, root_path, class: :comment_user %> 53 </li> 54 <% end %> 55 <%# // 投稿に紐づくコメントを一覧する処理を記述する %> 56 </ul> 57 </div> 58 </div> 59 </div> 60</main>

views/users/show.html.erb

ruby

1<div class="main"> 2 <div class="inner"> 3 <div class="user__wrapper"> 4 <h2 class="page-heading"> 5 <%= "ユーザー名さんの情報"%> 6 </h2> 7 <table class="table"> 8 <tbody> 9 <tr> 10 <th class="table__col1">名前</th> 11 <td class="table__col2"><%= "ユーザー名" %></td> 12 </tr> 13 <tr> 14 <th class="table__col1">プロフィール</th> 15 <td class="table__col2"><%= "ユーザーのプロフィール" %></td> 16 </tr> 17 <tr> 18 <th class="table__col1">所属</th> 19 <td class="table__col2"><%= "ユーザーの所属" %></td> 20 </tr> 21 <tr> 22 <th class="table__col1">役職</th> 23 <td class="table__col2"><%= "ユーザーの役職" %></td> 24 </tr> 25 </tbody> 26 </table> 27 <h2 class="page-heading"> 28 <%= "ユーザー名さんのプロトタイプ"%> 29 </h2> 30 <div class="user__card"> 31 <%# 部分テンプレートでそのユーザーが投稿したプロトタイプ投稿一覧を表示する %> 32 </div> 33 </div> 34 </div> 35</div>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2021/04/12 04:43

> @user = User.find(params[:id]) とあるので、パラメータとしてユーザのIDを渡すようにリンク作ればいけそうに思いますが、現在どのように作っているつもりなのでしょうか。 現状の自身の書かれたコードの理解度も書いていただきたいです。
winterboum

2021/04/12 22:50

「うまく表示されません。」と否定形では何もわかりません。 「どのようになってほしいのに」 「このようになった」 という情報をください
guest

回答1

0

ベストアンサー

見たところ routes も link_to も controller も出来てるので views/users/show.html.erb を以下のように直せば良いような気がしますが・・・そういう話ではなくですか?

erb

1<%= "ユーザー名さんの情報"%> 23<%= "#{@user.name}さんの情報"%> 4 5 6<td class="table__col2"><%= "ユーザー名" %></td> 78<td class="table__col2"><%= @user.name %></td> 9 10 11<td class="table__col2"><%= "ユーザーのプロフィール" %></td> 1213<td class="table__col2"><%= @user.profile %></td> 14 15(以下略)

質問者さんの意図をつかめていません。
的外れな回答だったらすみません。

投稿2021/04/12 23:53

shinoharat

総合スコア1680

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sydds

2021/04/13 00:18

私の拙い説明にも関わらず、意図を汲み取ってお答え頂きありがとうございます。 私の求めていた結果になりました。 本当にありがとうございます!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問