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

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

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

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

Q&A

解決済

3回答

2375閲覧

RoutingErrorの解決方法がわかりません。

ityou

総合スコア0

Ruby on Rails

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

0グッド

0クリップ

投稿2021/02/10 08:31

前提・実現したいこと

コメント機能の実装中にRoutingErrorが出ました。
本来であれば送信ボタンを押すとデータが保存されて、一覧で表示されるはずですが、エラーが出て先へ進めません。

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

Routing Error No route matches [POST] "/prototypes/1" Rails.root: /Users/miwanotakeshitooru/projects/protospace-33011 中略 Request Parameters: {"authenticity_token"=>"yL+3VXly0cJvCWPFQt7fKhcH3im4uWwETsx9uv/GnX0YGWmZltnIpE9CihcvfqlJqrMInEDOR9JH+4MH81qMIg==", "text"=>"", "commit"=>"送信する"}

該当のソースコード

ルーティング

Rails.application.routes.draw do devise_for :users root to: "prototypes#index" resources :prototypes, only: [:new, :create, :show, :edit, :update, :destroy] resources :comment, only: [:create ] end

ビュー

HTML

1<main class="main"> 2 <div class="inner"> 3 <div class="prototype__wrapper"> 4 <p class="prototype__hedding"> 5 <%= @prototype.title %> 6 </p> 7 <%= link_to "by #{@prototype.user.name}", root_path, class: :prototype__user %> 8 <%# プロトタイプの投稿者とログインしているユーザーが同じであれば以下を表示する %> 9 <% if current_user == @prototype.user %> 10 <div class="prototype__manage"> 11 <%= link_to "編集する", edit_prototype_path(@prototype), class: :prototype__btn %> 12 <%= link_to "削除する", prototype_path(@prototype), method: :delete, class: :prototype__btn %> 13 </div> 14 <% end %> 15 <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> 16 <div class="prototype__image"> 17 <%= image_tag @prototype.image %> 18 </div> 19 <div class="prototype__body"> 20 <div class="prototype__detail"> 21 <p class="detail__title">キャッチコピー</p> 22 <p class="detail__message"> 23 <%= @prototype.catch_copy %> 24 </p> 25 </div> 26 <div class="prototype__detail"> 27 <p class="detail__title">コンセプト</p> 28 <p class="detail__message"> 29 <%= @prototype.concept %> 30 </p> 31 </div> 32 </div> 33 <div class="prototype__comments"> 34 <%# ログインしているユーザーには以下のコメント投稿フォームを表示する %> 35 <% if user_signed_in? %> 36 <%= form_with model: [@prototype, @comment], local: true do |f|%> 37 <div class="field"> 38 <%= f.label :text, "コメント" %><br /> 39 <%= f.text_field :text %> 40 </div> 41 <div class="actions"> 42 <%= f.submit "送信する", class: :form__btn %> 43 </div> 44 <% end %> 45 <% end %> 46 <%# // ログインしているユーザーには上記を表示する %> 47 <ul class="comments_lists"> 48 <%# 投稿に紐づくコメントを一覧する処理を記述する %> 49 <li class="comments_list"> 50 <% @comments.each do |comment| %> 51 <%= comment.text%> 52 <%= link_to "(#{comment.user.name})", user_path(comment.user), class: :comment_user %> 53 <% end %> 54 </li> 55 56 <%# // 投稿に紐づくコメントを一覧する処理を記述する %> 57 </ul> 58 </div> 59 </div> 60 </div> 61</main>

コントローラー

Ruby

1class CommentsController < ApplicationController 2 def create 3 @comment = Comment.new(comment_params) 4 if @comment.save 5 redirect_to prototype_path(@comment.prototype) 6 else 7 @prototype = @comment.prototype 8 @comments = @prototype.comments 9 render "prototypes/show" 10 end 11 end 12 13 private 14 15 def comment_params 16 params.require(:comment).permit(:text).merge(user_id: current_user.id, prototype_id: params[:prototype_id]) 17 end 18end

rails routes

Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit user_registration PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy POST /users(.:format) devise/registrations#create root GET / prototypes#index prototypes POST /prototypes(.:format) prototypes#create new_prototype GET /prototypes/new(.:format) prototypes#new edit_prototype GET /prototypes/:id/edit(.:format) prototypes#edit prototype GET /prototypes/:id(.:format) prototypes#show PATCH /prototypes/:id(.:format) prototypes#update PUT /prototypes/:id(.:format) prototypes#update DELETE /prototypes/:id(.:format) prototypes#destroy comment_index POST /comment(.:format) comment#create 後略

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

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

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

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

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

winterboum

2021/02/10 09:56

view は Prototipe の show ですか? app/views のさきは何なんか教えてください。。 それと そのviewを呼び出すcontrollerのcodeを載せてください
guest

回答3

0

POST /prototypes/:id/comments というパスが必要に見えます。

ルーティングを以下のようにしてください。

ruby

1resources :prototypes, only: [:new, :create, :show, :edit, :update, :destroy] do 2 resources :comments, only: [:create] 3end

投稿2021/02/10 10:51

neko_daisuki

総合スコア2090

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

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

0

自己解決

モデルに記載されたバリデーションの誤字が原因でした。

prototypes → prototype

回答してくださった皆様ありがとうございました。

投稿2021/02/11 03:58

ityou

総合スコア0

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

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

0

ひとまず、formのmethod:getを明示されてみては。

投稿2021/02/10 08:41

m.ts10806

総合スコア80861

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

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

ityou

2021/02/10 09:21

すみません、本当に素人で申し訳ないのですがformのmethod:getというのはなんでしょうか? ビューの<%= form_with model: [@prototype, @comment], local: true do |f|%>のことですか?
m.ts10806

2021/02/10 10:08

formタグのmethod属性がPOSTだから/prototypes/1に対して存在しないPOSTリクエストを送信しているものと思われます。 用途からすると更新するわけではなく、あくまでデータ表示だけのようなのですし ルーティング/prototypes/1に対してGETメソッドはあるようなので method属性がGETになるように調整してみては、という提案をしています。 ※もしわからなければ、「リクエスト」部分とHTMLのformのことをきちんとおさえてください。分からないと前に進めませんしアドバイスが理解できません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問