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

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

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

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

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

Q&A

1回答

1092閲覧

コメントを保存できない DBにも保存できない 

OKURA

総合スコア0

Ruby

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

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

0グッド

0クリップ

投稿2022/02/27 16:33

コメント機能を実装しようとしているコメント投稿のフォームに投稿したが
詳細ページが更新されるだけで
データベースには保存されていない
リロードすると
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

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

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

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

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

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

winterboum

2022/02/27 22:49 編集

パット見 codeに問題見えないので、comment投稿したときの logがあると良いです。 もしくは create! にして投稿してその結果でも。 モデルCommentのcodeも。 わたしこのあと3/2深夜までアクセスできないので、回答は他の方を期待して。
guest

回答1

0

@snaptik おっしゃる通り、コメントがデータベースに保存されていないため、詳細ページが更新されるだけで、リロードするとエラーが出ているようですね。このエラーは、コメントの投稿先である/prototypes/1/commentsというURLが存在しないために発生しています。この問題を解決するためには、routes.rbファイルにresources :commentsというルーティングを追加する必要があります。これにより、/prototypes/1/commentsというURLが有効になり、コメントの投稿が可能になります。また、show.html.erbのコードには、コメントの投稿フォームが含まれていることがわかりました。ログインしているユーザーには、このフォームが表示されます。コメントの投稿フォームが表示されない場合は、ログインしていない可能性があります。ログインしていない場合は、ログインしてから再度アクセスしてください。以上の情報がお役に立てれば幸いです。

投稿2024/01/22 09:59

Margao

総合スコア6

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問