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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

1972閲覧

コメントを保存したいがルーティングエラーが発生して保存できない。

gomappi

総合スコア7

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/01/14 12:20

前提・実現したいこと

commentを保存したい。

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

Routing Error No route matches [POST] "/prototypes/12"

該当のソースコード

app/views/prototypes/show.html

ruby

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 user_signed_in? && current_user.id == @prototype.user_id %> 10 <div class="prototype__manage"> 11 <%= link_to "編集する", edit_prototype_path(@prototype.id), class: :prototype__btn %> 12 <%= link_to "削除する", prototype_path, class: :prototype__btn, method: :delete%> 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 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 <%= " コメントのテキスト "%> 51 <%= link_to "( ユーザー名 )", root_path, class: :comment_user %> 52 </li> 53 <%# // 投稿に紐づくコメントを一覧する処理を記述する %> 54 </ul> 55 </div> 56 </div> 57 </div> 58</main>

config/routes.rb

ruby

1Rails.application.routes.draw do 2 devise_for :users 3 root to:'prototypes#index' 4 5 resources :prototypes do 6 resources :comments, only: :create 7 end 8 9end

ルーティング

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 prototype_comments POST /prototypes/:prototype_id/comments(.:format) comments#create prototypes GET /prototypes(.:format) prototypes#index 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

models/prototype

class Prototype < ApplicationRecord belongs_to :user has_many :comments , dependent: :destroy has_one_attached :image validates :title , presence: true validates :catch_copy , presence: true validates :concept , presence: true validates :image, presence: true end

models/comment

class Comment < ApplicationRecord belongs_to :user belongs_to :prototype validates :text , presence: true end

db/✖️✖️✖️✖️_create_comments

class CreateComments < ActiveRecord::Migration[6.0] def change create_table :comments do |t| t.text :text t.timestamps t.references :user, foreign_key: true t.references :prototype, foreign_key: true end end end

controllers/comments_contoroller

class CommentsController < ApplicationController def create @comment = Comment.new(comment_params) if @comment.save redirect_to prototype_comments_path(@comment.prototype) else @prototype = @comment.prototype @comments = @prototype.comments render "prototype/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

試したこと

app/views/prototypes/show.htmlのコメント送信するform_withの部分にurlが書かれていないからルートが正しく通っていないと考察し、ルーティングのパスを参考に記述したが変わらなかった。

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

Rails 6.0.3.4
ruby 2.6.5

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

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

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

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

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

m.ts10806

2021/01/14 12:24

確かにルーティングの /prototypes/:id にPOSTがないですね。 というところは把握の上でしょうか。 設計的には何で処理させるつもりですか?
gomappi

2021/01/14 12:34

prototype_comments POST /prototypes/:prototype_id/comments(.:format) comments#create で処理したいと思うのですが、うまくルートが組めません。この返答で問題ないでしょうか? よろしくお願いいたします。
gomappi

2021/01/14 13:11

ルーティングに入力して実行しました。 https://gyazo.com/faf602b57df82521efc4c13ea99295d2 と表示されました。今度はコメントが空になっているのか、paramsがないのかという意味だと思うのですが、どうでしょうか?
m.ts10806

2021/01/14 13:25

あとはデバッグですね。
gomappi

2021/01/14 13:29

そうですね。ルーティングには成功していますのでデバッグを頑張ってみます。
guest

回答1

0

自己解決

ルーティング

post '/prototypes/:id', to: 'comments#create'

を追加。

投稿2021/01/14 13:36

gomappi

総合スコア7

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問