taccc_
@taccc_

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

[質問]NoMethodError in Prototypes#showエラーについて

Q&A

Closed

解決したいこと

railsを使用して、写真投稿アプリを作成しているのですが、コメントの情報をshowアクションに表示刺せ料とした際に"NoMethodError in Prototypes#show"エラーが出てしまっているので解決したいです。
値が入っていないようなのはわかるのですが、いろいろ試しても解消されず、どのように原因を見つけたらいいのかご教授していただければ幸いです。

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

スクリーンショット 2020-10-18 21.08.54.png

該当するソースコード

#show.html.erb
<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, class: :prototype__btn %>
          <%= link_to "削除する", prototype_path(@prototype.id), method: :delete, class: :prototype__btn %>
        </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 current_user %>
          <%= form_with(model: [@prototype, Comment.new], local: true) do |f| %>
            <div class="field">
              <%= f.label :text, "コメント" %><br />
              <%= f.text_field :text %>
            </div>
            <div class="actions">
              <%= f.submit "送信する", class: :form__btn  %>
            </div>
          <% end %> 
        <% end %>
        <%# // ログインしているユーザーには上記を表示する %>
        <ul class="comments_lists">
          <%# 投稿に紐づくコメントを一覧する処理を記述する %>
            <li class="comments_list">

              <%= @comments.text %>
              <%= link_to @comments.user.name, root_path, class: :comment_user %>
            </li>
          <%# // 投稿に紐づくコメントを一覧する処理を記述する %>
        </ul>
      </div>
    </div>
  </div>
</main>

#prototypes_controller.rb
class PrototypesController < ApplicationController
  protect_from_forgery with: :null_session
  def index
    @prototypes = Prototype.includes(:user).order("created_at DESC")
  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
    @comments = Comment.find_by(id:params[:id], prototype_id: params[:prototype_id])     
  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])
    if prototype.destroy
      redirect_to root_path
    end    
  end

  private
  def prototype_params
    params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id)
  end
end
#model/comment.rb 
class Comment < ApplicationRecord

  belongs_to :users
  belongs_to :prototypes

  validates :text,     presence: true
end
#model/prototype.rb
class Prototype < ApplicationRecord
  belongs_to :user
  has_one_attached :image
  has_many :comments, dependent: :destroy

  validates :title,       presence: true 
  validates :catch_copy,  presence: true
  validates :concept,     presence: true
  validates :image,       presence: true
end
0

No Answers yet.

Your answer might help someone💌