前提・実現したいこと
前回の質問に引き続きコメント添付付き画像投稿アプリを作成しています。
コメント機能の実装を行なっているのですが以下のエラーが出てしまいデータベースにコメントが保存されていない状態です。
何をやっても先に進みまない状態になってしまったのでお力を貸していただければと思います。
発生している問題・エラーメッセージ
NoMethodError in Comments#create undefined method `title' for nil:NilClass <div class="prototype__wrapper"> <p class="prototype__hedding"> <%= @prototype.title %>←ここがエラー箇所と出ています </p> <%= link_to @prototype.user.name, root_path, class: :prototype__user %>
titleの情報が入って無いと言っているようですが恐らくそことは別の場所にエラーがあると考えています。
該当のソースコード
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, 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), method: :get, 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) %> 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 :comment, "コメント" %><br /> 39 <%= f.text_field :comment, id:"comment_content" %> 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 <%# <%= " コメントのテキスト "%> 51 <%# <%= link_to "( ユーザー名 )", root_path, class: :comment_user %> 52 </li> 53 <%# // 投稿に紐づくコメントを一覧する処理を記述する %> 54 </ul> 55 </div> 56 </div> 57 </div> 58</main> 59
prototypes_controller.rb
class PrototypesController < ApplicationController def index @prototypes = Prototype.all end def new @prototype = Prototype.new end def create prototype = Prototype.new(prototype_params) if prototype.save redirect_to root_path else render :new end end def show @prototype = Prototype.find(params[:id]) @comment = Comment.new end def edit @prototype = Prototype.find(params[:id]) end def update @prototype = Prototype.find(params[:id]) if @prototype.update(prototype_params) redirect_to prototype_path else render :edit end end def destroy prototype = Prototype.find(params[:id]) prototype.destroy redirect_to root_path end private def prototype_params params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) end end
comments_controller.rb
class CommentsController < ApplicationController def create @comment = Comment.new(comment_params) if @comment.save redirect_to prototype_path(@comment.prototype) else @prototype = @comment.prototype @comments = @prototype.comments render "prototypes/show" end end private def comment_params params.require(:comment).permit(:text).merge(user_id: current_user.id, prototype_id: params[:prototype_id]) end end
routes.rb
Rails.application.routes.draw do devise_for :users root to: 'prototypes#index' resources :prototypes do resources :comments, only: :create end end
試したこと
他に似たような質問者様がいらっしゃいましたので参考にさせて頂いたのですがどれも解決には至りませんでした。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/11/19 04:35