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

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

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

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

Ruby on Rails

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

バージョン管理

バージョン管理はコンピューター上にファイルとして格納されているドキュメント・プログラム・その他の情報の変更履歴等を管理するものです

Q&A

解決済

2回答

1770閲覧

# NoMethodErrorを解決したい。ユーザー名をクリックした時に、ユーザー詳細画面へ飛びたい。

hiromu-t3966

総合スコア6

Ruby

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

Ruby on Rails

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

バージョン管理

バージョン管理はコンピューター上にファイルとして格納されているドキュメント・プログラム・その他の情報の変更履歴等を管理するものです

0グッド

1クリップ

投稿2020/11/14 14:09

前提・実現したいこと

簡易的な画像投稿アプリを作っています。
コメントしたユーザー名をクリックすると、そのユーザーの詳細ページへ飛びたいのですが上手くいきません。

発生している問題・エラーメッセージ

イメージ説明

該当のソースコード

ruby

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

ruby

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

ruby

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

試したこと

app>views>prototypes>show.html.erbのapp>views>prototypes>show.html.erbでエラーが出たのでパスの指定を間違えていないか確認した。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答2

0

def show の @user = User.find(params[:id]) でエラーが無いということは@userは居る、ということなので、これがnilではなさそう。

とすると、UsersControllerのshowではないところから呼ばれていませんか?
このエラーがでたのは、どの画面でどういう操作をした時でしょう。

投稿2020/11/14 23:02

winterboum

総合スコア23408

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

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

0

ベストアンサー

user_path(@user.id)これがuser_path(comment.user.id)になると動くかと思いました

投稿2020/11/14 17:44

hatsu

総合スコア1809

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問