LoginSignup

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

コメントが保存できない and  定義したインスタンス変数が空になって表示できない

解決したいこと

rails commentをcreateアクションで保存させたい

発生している問題・エラー

NoMethodError in Comments#create

undefined method `title' for nil:NilClass

ビュー内のインスタンス変数を記述した場所で出ています

以下はソースコード

class CommentsController < ApplicationController
  def create #コメントが保存できた時の処理
    @comment = Comment.new(comment_params)
    if @comment.save  #できなかった時の処理
       redirect_to prototype_path(@comment.prototype)
    else
      render "prototypes/show"
    end
  end

  private

  def comment_params
    params.require(:comment).permit(:content).merge(user_id: current_user.id, prototype_id: params[:prototype_id])
  end
end

#省略
class PrototypesController < ApplicationController
  before_action :authenticate_user!


  def show
    @comment = Comment.new
    @prototype = Prototype.find(params[:id])
    
  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

#省略


<main class="main">
  <div class="inner">
    <div class="prototype__wrapper">
      <p class="prototype__hedding">
        <%= @prototype.title %> ########ここでエラーが出ています
      </p>
      <%= link_to @prototype.user.name, root_path, class: :prototype__user %>
      <%# プロトタイプの投稿者とログインしているユーザーが同じであれば以下を表示する %>
      <% if user_signed_in? && current_user.id == @prototype.user_id %>
        <div class="prototype__manage">
          <%= link_to "編集する", edit_prototype_path(@prototype), class: :prototype__btn %>
          <%= link_to "削除する", prototype_path(@prototype), class: :prototype__btn, method: :delete %>
        </div>
      <% end %>  
      <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %>
      <div class="prototype__image">
        <%= image_tag @prototype.image %>
      </div>
      <div class="prototype__body">
        <div class="prototype__detail">
          <p class="detail__title">キャッチコピー</p>
          <p class="detail__message">
            <%=  @prototype.catch_copy %>
          </p>
        </div>
        <div class="prototype__detail">
          <p class="detail__title">コンセプト</p>
          <p class="detail__message">
            <%= @prototype.concept %>
          </p>
        </div>
      </div>
      <div class="prototype__comments">
        <%# ログインしているユーザーには以下のコメント投稿フォームを表示する %>
        <% if user_signed_in? %>
          <%= form_with model: @comment, local: true do |f|%>
            <div class="field">
              <%= f.label :content, "コメント" %><br />
              <%= f.text_field :content, id:"comment_content" %>
            </div>
            <div class="actions">
              <%= f.submit "送信する", class: :form__btn  %>
            </div>
          <% end %>
        <% end %>  
        <%# // ログインしているユーザーには上記を表示する %>
        <ul class="comments_lists">
          <%# 投稿に紐づくコメントを一覧する処理を記述する %>
            <li class="comments_list">
              <%# <%= " コメントのテキスト "%>
              <%# <%= link_to "( ユーザー名 )", root_path, class: :comment_user %>
            </li>
          <%# // 投稿に紐づくコメントを一覧する処理を記述する %>
        </ul>
      </div>
    </div>
  </div>
</main>

自分で試したこと

エラーから読み取れること
ビュー内で記述したインスタンス変数がからになっている事から
ビューに表示できないと仮定

showアクションでインスタンス変数を定義しているのに
なぜアクション終了後にからになっているのか知りたいです
また、コメントも保存されないので原因が今の所分かりません

どうかご教授ください

0

1Answer

質問内容を変える場合は削除して作り直すのではなく内容を編集してみてはいかがでしょうか。経緯が分からなくなるので。

0Like

Your answer might help someone💌