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

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

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

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

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

740閲覧

No route matches [PATCH]

moto12

総合スコア15

Ruby

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

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/11/06 09:33

前提・実現したいこと

チャットルームアプリ作成中で、コメント(comment)に対して返答(response)をしようとしたところ、routing errorが出てきました。

私がしたいことはPost(responseの保存)なのに、なぜPatchになっているのか分かりません。
ルーティングの設定自体は間違っていないような気がするのですが...

【エラーメッセージ画面のGyazo】
https://gyazo.com/7a736db0077689339ee47b8b60c31d63

発生している問題・エラーメッセージ

エラーメッセージ Routing Error No route matches [PATCH] "/comments/50/responses"

該当のソースコード

Ruby

1【response の new.html.erb】 2 3<div class="comment"> 4 <div class="comment-box"> 5 <%= form_with model: [@response, @comment],url:comment_responses_path(@comment),local: true do |f| %> 6 <%= f.text_area :response_text, placeholder: "質問する", class: "comment-text" %> 7 <%= hidden_field_tag :comment_id,@comment.id %> 8 <p class="comment-warn"> 9 <p>他の生徒の質問で回答できる場合には積極的に回答してください!</p> 10 </p> 11 <br> 12 <%= f.submit "質問する",class: "comment-btn"%> 13 <div class="back-to-top2"><%=link_to '戻る', root_path %></div> 14 <% end %> 15 </div> 16 17<div class="comment-box2"> 18 <h4 class="comment-list"><質問一覧></h4> 19 <% if @responses %> 20 <% @responses.each do |response| %> 21 <div class="comment-content"> 22 <% if current_user.genre == "教員" %> 23 <p> 24 <strong><%= link_to comment.user.name, "#", class: "comment-nickname" %></strong> 25 </p> 26 <% elsif current_user.genre == "生徒" %> 27 <strong class= "comment-nickname">匿名</strong> 28 <% end %> 29 <p class="comment-texts"> 30 <%=link_to comment.text, new_comment_response_path(comment) %> 31 </p> 32 <ul class="comment-time"> 33 <p class="time"><%= response.created_at.to_s(:datetime_jp) %></p> 34 <p class="time"><%= response.created_at.to_s(:datetime_jp2) %></p> 35 </ul> 36 <% if current_user.genre == "教員" %> 37 <p> 38 <%= link_to message_comment_path(@comment.id, response.id), method: :delete, class: "delete-button" do %> 39 <i class="fas fa-trash"></i> 40 <% end %> 41 </p> 42 <% elsif user_signed_in? && current_user.id == response.user_id %> 43 <p> 44 <%= link_to message_comment_path(@comment.id, response.id), method: :delete, class: "delete-button" do %> 45 <i class="fas fa-trash"></i> 46 <% end %> 47 </p> 48 <% end %> 49 <p> 50 </div> 51 <% end %> 52 <% end %> 53 </div> 54 </div> 55 56

Ruby

1class ResponsesController < ApplicationController 2 3 def new 4 @comment = Comment.find(params[:comment_id]) 5 @response = Response.new 6 @responses = @comment.responses.order("created_at DESC") 7 @room = Room.all 8 end 9 10 def create 11 @comment = Comment.find(params[:comment_id]) 12 @response = @comment.responses.new(response_params) 13 if @response.save 14 redirect_to new_comment_response_path(@comment) 15 else 16 redirect_to new_comment_response_path(@comment) 17 end 18 end 19 20 def destroy 21 @response = Response.find(params[:id]) 22 @comment = @response.comment 23 if @response.destroy 24 redirect_to new_comment_response_path(@comment) 25 else 26 redirect_to new_comment_response_path(@comment) 27 end 28 end 29 30 private 31 def response_params 32 params.require(:response).permit(:response_text).merge(user_id: current_user.id, comment_id: params[:comment_id]) 33 end 34 35end 36

Ruby

1【routes.rb】 2 3Rails.application.routes.draw do 4 devise_for :users 5 get 'messages/index' 6 get 'comments/index' 7 8 root to: "rooms#index" 9 resources :users 10 resources :rooms do 11 resources :messages 12 end 13 resources :messages do 14 resources :comments 15end 16 17 resources :comments do 18 resources :agrees, only: [:create, :destroy] 19end 20 21 resources :comments do 22 resources :responses, only: [:new, :create, :destroy] 23end 24 25end 26

Ruby

1comment_responses POST /comments/:comment_id/responses(.:format) responses#create 2new_comment_response GET /comments/:comment_id/responses/new(.:format) responses#new 3comment_response DELETE /comments/:comment_id/responses/:id(.:format) responses#destroy

Ruby

Ruby

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

form_with model: [@response, @comment] なので
@comment = Comment.find(params[:comment_id]) のcommentに対してURLなどが決まったのかな。

コメント(comment)に対して返答(response) なら
form_with model: [@comment, @response] ではないかと

投稿2020/11/06 13:31

winterboum

総合スコア23376

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問