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

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

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

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

Q&A

解決済

1回答

873閲覧

Routing Errorが出ています

Estt

総合スコア1

Ruby

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

0グッド

0クリップ

投稿2020/12/04 14:21

Ruby初心者です。
ツイートに対するコメント投稿機能を実装しようとしているのですが、コメントを投稿すると以下のようなエラーが表示されコメントを表示できません。
rails routesで確認してもHTTPメソッドPOSTのprototypes/createアクションのルーティングは表示されています。

No route matches [POST] "/prototypes/2"

routes.rb

Rails.application.routes.draw do devise_for :users root to: "prototypes#index" resources :prototypes do resources :comments, only: [:create] end end

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}", root_path, class: :prototype__user %> <%# プロトタイプの投稿者とログインしているユーザーが同じであれば以下を表示する %> <% if user_signed_in? && current_user.id == @prototype.user_id %> <div class="prototype__manage"> <%= link_to "編集する", edit_prototype_path, class: :prototype__btn %> <%= link_to "削除する", prototype_path, class: :prototype__btn, method: :delete %> </div> <% end %> <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> <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"> <%# ログインしているユーザーには以下のコメント投稿フォームを表示する %> <%= form_with local: true do |f|%> <div class="field"> <%= f.label :comment, "コメント" %><br /> <%= f.text_field :comment %> </div> <div class="actions"> <%= f.submit "送信する", class: :form__btn %> </div> <% end %> <%# // ログインしているユーザーには上記を表示する %> <ul class="comments_lists"> <%# 投稿に紐づくコメントを一覧する処理を記述する %> <li class="comments_list"> <%= @comment.text %> <%= link_to @comment.user, root_path, class: :comment_user %> </li> <%# // 投稿に紐づくコメントを一覧する処理を記述する %> </ul> </div> </div> </div> </main>

comments_controller.rb

class CommentsController < ApplicationController before_action :authenticate_user! def create if comment = Comment.create(comment_params) redirect_to "/prototypes/#{comment.prototype.id}" else render :index end end private def comment_params params.require(:comment).permit(:text).merge(user_id: current_user.id, prototype_id: params[:prototype_id]) end end

prototypes_controller.rb

class PrototypesController < ApplicationController before_action :authenticate_user! def index @prototypes = Prototype.all end def new @prototype = Prototype.new end def create if Prototype.create(prototype_params) redirect_to root_path else render :index 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 prototype_path else render :edit end end def destroy prototype = Prototype.find(params[:id]) prototype.destroy redirect_to root_path end private def prototype_params params.require(:prototype).permit(:title,:catch_copy,:concept,:image).merge(user_id: current_user.id) end end

application_controller.rb

class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? private def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:name,:profile,:occupation,:position]) end end

commentモデル

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

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

userモデル

class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable validates :name, presence: true validates :profile, presence: true validates :occupation, presence: true validates :position, presence: true has_many :prototypes has_many :comments end

その他必要なファイルがあればなんなりとお申し付けください。
お手数おかけしますが、どうぞよろしくお願い致します。
環境
rails 6.0.3.4
ruby 2.6.5

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

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

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

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

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

guest

回答1

0

自己解決

postのルーティングを設定して解決しました。

投稿2020/12/04 16:45

Estt

総合スコア1

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問