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

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

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

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

Q&A

解決済

1回答

3225閲覧

NoMethodError in Comments#createを解決したい

sydds

総合スコア2

Ruby

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

0グッド

0クリップ

投稿2021/04/11 07:09

前提・実現したいこと

コメント投稿機能の実装を行っている最中にエラーが出てしまい前進できずにいます。
初めての投稿でいたらない点があるかもしれませんが、よろしくお願いします。

調べたところ、titleが定義されていないという意味かと思うのですが、どこが原因なのか解決できずに困っています。

エラーメッセージ

NoMethodError in Comments#create Showing /Users/sh/projects/protospace-35238/app/views/prototypes/show.html.erb where line #5 raised: undefined method `title' for nil:NilClass

ruby

1 <div class="prototype__wrapper"> 2 <p class="prototype__hedding"> 3 <%= @prototype.title %> 4 </p> 5 <%= link_to "by #{@prototype.user.name}", root_path, class: :prototype__user %> 6 7Request 8Parameters: 9 10{"authenticity_token"=>"Id7GhontoT3Yf3U8DCziWsfHnsrfWQsDXvST1K8TjORUfSSeeXzb//QeiGwF4D5GUWrItuzJwoJCf2oOZCuNEg==", "comment"=>{"text"=>""}, "commit"=>"送信する", "prototype_id"=>"1"} 11

コード

ptorotypes_controller

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

comment_controller

ruby

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

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

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

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

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

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

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

guest

回答1

0

ベストアンサー

show を呼び出しているviewの作りによるのですが、どうやら id ではなく prototype_id を送ってきています。
{"authenticity_token"=>"Id7G...NEg==", "prototype_id"=>"1"}
showへの呼び出し先がここだけなら
def show @prototype = Prototype.find(params[:prototype_id])
とすれば済みます、
が、
一般的には id かと思うので、「showを呼び出しているview」を直して id を送るように直す、というのも手です。

id で送ってきたり、prototype_id で送って来たりの混在が残るようなら
@prototype = Prototype.find(params[:prototype_id] || params[:id])

投稿2021/04/11 07:41

winterboum

総合スコア23401

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

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

sydds

2021/04/11 07:53

ありがとうございます。解決できました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問