コメント機能を実装しようとしているコメント投稿のフォームに投稿したが
詳細ページが更新されるだけで
データベースには保存されていない
リロードすると
No route matches [GET] "/prototypes/1/comments"が出る。
コード show.html.erb ``<main class="main"> <div class="inner"> <div class="prototype__wrapper"> <p class="prototype__hedding"> <%= @prototype.title %> </p> <%= link_to "by #{@prototype.user.name}", "/users/#{@prototype.user.id}", class: :prototype__user %> <%# プロトタイプの投稿者とログインしているユーザーが同じであれば以下を表示する %> <div class="prototype__manage"> <%= link_to "編集する", edit_prototype_path(@prototype.id), class: :prototype__btn %> <%= link_to "削除する", prototype_path(@prototype.id), method: :delete, class: :prototype__btn %> </div> <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> <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: [@prototype,@comment],local: true do |f|%> <div class="field"> <%= f.label :content, "コメント" %><br /> <%= f.text_field :content %> </div> <div class="actions"> <%= f.submit "送信する", class: :form__btn %> </div> <% end %> <% end %> <%# // ログインしているユーザーには上記を表示する %> <ul class="comments_lists"> <%# 投稿に紐づくコメントを一覧する処理を記述する %> <% @comments.each do |comment| %> <li class="comments_list"> <%= comment.content%> <%= link_to "(#{comment.user.name})", user_path(comment.user), root_path, class: :comment_user %> </li> <% end %> <%# // 投稿に紐づくコメントを一覧する処理を記述する %> </ul> </div> </div> </div> </main> ```ここに言語を入力 コード prototype_controller.rb ``class PrototypesController < ApplicationController before_action :authenticate_user!, except: [:index, :show] before_action :move_to_index, only: [:edit] 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 @comments = @prototype.comments.includes(:user) end def edit @prototype = Prototype.find(params[:id]) end def update @prototype = Prototype.find(params[:id]) if @prototype.update(prototype_params) redirect_to root_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 def move_to_index @prototype = Prototype.find(params[:id]) unless current_user.id == @prototype.user.id redirect_to root_path end end end ```ここに言語を入力 コード comments_controller ```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 get 'comment/create' devise_for :users get 'prototypes/index' root to: "prototypes#index" resources :prototypes do resources :comments, only: :create end resources :users, only: :show end
パット見 codeに問題見えないので、comment投稿したときの logがあると良いです。
もしくは create! にして投稿してその結果でも。
モデルCommentのcodeも。
わたしこのあと3/2深夜までアクセスできないので、回答は他の方を期待して。