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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

2102閲覧

コメント投稿者の名前を表示したい

bonky

総合スコア2

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/07/11 14:36

編集2021/07/12 13:05

前提・実現したいこと

コメントの投稿機能の実装中です。
コメント一覧にて、コメント投稿者の名前を表示したいのですが、エラーが発生してしまいました。
undefined method `name' for nil:NilClass とあるのでnameが空ということだと思うのですが、
思考錯誤してみましたが、進めずにいるので質問させていただきます。よろしくお願いします。

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

NoMethodError in Prototypes#show
Showing /Users/nk/projects/protospace-35366/app/views/prototypes/show.html.erb where line #54 raised:

undefined method `name' for nil:NilClass

ruby

1 52 <%= comment.text%> 2 53 </li> 3 54 <%= link_to @comment.user.name, user_path(comment.user), class: :comment_user %>この行です 4 55 <% end %> 5 56 </li> 6 <%# // 投稿に紐づくコメントを一覧する処理を記述する %>

Rails.root: /Users/nk/projects/protospace-35366

Application Trace | Framework Trace | Full Trace
app/views/prototypes/show.html.erb:54
app/views/prototypes/show.html.erb:50
Request
Parameters:

{"id"=>"22"}

該当のソースコード

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 @prototype.user.name, user_path(@prototype.user), class: :prototype__user %> 8 <%# プロトタイプの投稿者とログインしているユーザーが同じであれば以下を表示する %> 9 <div class="prototype__manage"> 10 <% if user_signed_in? && current_user.id == @prototype.user_id %> 11 <%= link_to "編集する", edit_prototype_path(@prototype.id), class: :prototype__btn %> 12 <%= link_to "削除する", prototype_path(@prototype.id), method: :delete, class: :prototype__btn %> 13 <% end %> 14 </div> 15 <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> 16 <div class="prototype__image"> 17 <%= image_tag @prototype.image%> 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 <%= form_with model: [@prototype, @comment], local: true do |f|%> 36 <% if user_signed_in? %> 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 <li class="comments_list"> 50 <% @comments.each do |comment| %> 51 <li class="comments_list"> 52 <%= comment.text%> 53 </li> 54 <%= link_to @comment.user.name, user_path(comment.user), class: :comment_user %> 55 <% end %> 56 </li> 57 <%# // 投稿に紐づくコメントを一覧する処理を記述する %> 58 </ul> 59 </div> 60 </div> 61 </div> 62</main>

prototypes_controller.rb

ruby

1class PrototypesController < ApplicationController 2 before_action :authenticate_user!, except: [:index, :show] 3 before_action :set_prototype, except: [:index, :new, :create] 4 before_action :contributor_confirmation, only: [:edit, :update, :destroy] 5 def index 6 @prototypes = Prototype.all 7 end 8 9 def show 10 @prototype = Prototype.find(params[:prototype_id] || params[:id]) 11 @comment = Comment.new 12 @comments = @prototype.comments.includes(:user) 13 end 14 15 def edit 16 @prototype = Prototype.find(params[:id]) 17 end 18 19 def update 20 @prototype = Prototype.find(params[:id]) 21 if @prototype.update(prototype_params) 22 redirect_to prototype_path(@prototype.id) 23 else 24 render :edit 25 end 26 end 27 28 def destroy 29 prototype = Prototype.find(params[:id]) 30 prototype.destroy 31 redirect_to root_path(@user) 32 33 end 34 35 def new 36 @prototype = Prototype.new 37 end 38 39 def create 40 @prototype = Prototype.create(prototype_params) 41 if @prototype.save 42 redirect_to root_path(@user) 43 else 44 render :new 45 end 46 end

comments_controller.rb

ruby

1class CommentsController < ApplicationController 2 def create 3 @comment = Comment.new(comment_params) 4 if @comment.save 5 redirect_to prototype_path(@comment.prototype) 6 else 7 @prototype = @comment.prototype 8 @comments = @prototype.comments.includes(:user) 9 render "prototypes/show" 10 end 11 end 12 13 private 14 def comment_params 15 params.require(:comment).permit(:text).merge(user_id: current_user.id, prototype_id: params[:prototype_id]) 16 end 17end

users_controller.rb

ruby

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

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

ruby '2.6.5'

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

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

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

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

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

guest

回答1

0

ベストアンサー

nameが空なのではないです @comment.user.name の @comment.user が空なのです。
ここは @なしの comment.user.name が正しいのでは?

投稿2021/07/11 23:16

winterboum

総合スコア23401

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

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

bonky

2021/07/12 13:09

解決できました!ありがとうございます! よろしければもう少し詳しくお聞きできますでしょうか? 無知です。よろしくお願いします!
winterboum

2021/07/12 13:39

controller で定義されてるのは @comment = Coment.new ですから空です。 <% @comments.each do |comment| %> のloopの中のcomment一つ一つについての user.nameでしょうから、comment です
bonky

2021/07/12 15:15

勉強になりました! ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問