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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

780閲覧

Routing Errorを解決したい

sydds

総合スコア2

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/04/11 08:59

No route matches [GET] "/prototypes/1/comments"
とエラーが出てしまい困っております。

エラーについて調べて見るとprototypesとgetがマッチしていないとのことでした。

Rails routesで調べるとprototypesのgetは下記の通りprototypes#indexのところでした。

prototypes_index GET /prototypes/index(.:format) prototypes#index

コメントの投稿一覧を表示させようとしていた時にこのエラーが出たので何でなのかも分からず困っております。

routes.rb

ruby

1Rails.application.routes.draw do 2 devise_for :users 3 get 'prototypes/index' 4 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 5 root to: "prototypes#index" 6 resources :prototypes, only: [:index, :new, :create, :show, :edit, :update, :destroy] do 7 resources :comments, only: [:create] 8 end 9end 10

prototypeコントローラー

ruby

1class PrototypesController < ApplicationController 2 def index 3 @prototype = Prototype.all 4 end 5 6 def new 7 @prototype = Prototype.new 8 end 9 10 def create 11 @prototype = Prototype.new(prototype_params) 12 if @prototype.save 13 redirect_to root_path 14 else 15 render :new 16 end 17 end 18 19 def show 20 @prototype = Prototype.find(params[:id]) 21 @comment = Comment.new 22 @comments = @prototype.comments.includes(:user) 23 end 24 25 def edit 26 @prototype = Prototype.find(params[:id]) 27 end 28 29 def update 30 @prototype = Prototype.find(params[:id]) 31 if @prototype.update(prototype_params) 32 redirect_to action: "show" 33 else 34 render :edit 35 end 36 end 37 38 def destroy 39 prototype = Prototype.find(params[:id]) 40 prototype.destroy 41 redirect_to action: "index" 42 end 43 44 private 45 46 def prototype_params 47 params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) 48 end 49 50end 51

commentコントローラー

ruby

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

index.html.erb

ruby

1<main class="main"> 2 <div class="inner"> 3 <%# ログインしているときは以下を表示する %> 4 <% if user_signed_in? %> 5 <div class="greeting"> 6 <%= "こんにちは、" %> 7 <%= current_user.name %> 8 <%= "さん" %> 9 </div> 10 <% end %> 11 <%# // ログインしているときは上記を表示する %> 12 <div class="card__wrapper"> 13 <%# 投稿機能実装後、部分テンプレートでプロトタイプ投稿一覧を表示する %> 14 <%= render partial: "prototype", collection: @prototype %> 15 </div> 16 </div> 17</main>

show.html.rb

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(@prototype.id), method: :delete, class: :prototype__btn %> 13 </div> 14 <% end %> 15 <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> 16 <div class="prototype__image"> 17 <%= image_tag @prototype.image, class: :card__img %> 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, id:"comment_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 <% @comments.each do |comment| %> 50 <li class="comments_list"> 51 <%= link_to @prototype.user.name, root_path, class: :comment_user %> 52 <%= @prototype.comments %> 53 </li> 54 <%# // 投稿に紐づくコメントを一覧する処理を記述する %> 55 </ul> 56 </div> 57 </div> 58 </div> 59</main> 60

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

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

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

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

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

guest

回答1

0

自己解決

<% @comments.each do |comment| %>
に対するendの入力忘れでした。

投稿2021/04/12 04:32

sydds

総合スコア2

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問