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

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

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

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

Q&A

0回答

187閲覧

railsでNo route matches [POST] "/posts/39/comments/41"と出てしまう

KosukeYamamoto

総合スコア220

Ruby on Rails

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

0グッド

0クリップ

投稿2020/03/20 04:52

railsでカウントした数を表示させたいのですが、No route matches [POST] "/posts/39/comments/41"と出てしまいます。

routes

Rails.application.routes.draw do get 'static_pages/about' get 'static_pages/privacy' get 'static_pages/agreement' get 'static_pages/organization' delete 'users', to: 'users#destroy' resources :users do member do get :activate end end get 'registrations/new' resource :registrations, only: [:new, :create] resource :sessions, only: [:new, :create, :destroy] resource :settings, only: [:edit, :update] get 'settings/static_pages/about', to:'static_pages#about' resources :users, only: [:index, :show, :new] root to: 'posts#index' resources :users resources :posts do resources :comments do resources :likes, only: [:create, :destroy] end end resources :genres get 'attensions', to:'attensions#index' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end

showのビュー

<div class="container"> <div class="row"> <div class="answer"> <h4>回答</h4> <% if @post.comments.any? %> <ul> <% @post.comments.each do |comment| %> <% if @post.user == comment.user %> <% if @post.situation? %> <p>質問者</p> <% else %> <p>質問者:<%= @post.user %></p> <% end %> <% else %> <p>回答者:<%= @post.user.name %></p> <% end %> <li> <%= comment.body %> <% if @post.user == current_user %> <%= link_to '回答を削除する', post_comment_path(@post, comment), method: :delete, class: 'command', data: {confirm: '回答を削除しますか?' } %> <% end %> </li> <div class="evalation"> <% if current_user.already_liked?(@post) %> <%= button_to '♡', post_comment_path(@post,comment), method: :update, class: "button" %><%= comment.Evaluation %> <% else %> <% if comment.likes.count == 0 %> <%= button_to '♡', post_comment_path(@post,comment), method: :update, class: "button"%> <% else %> <%= button_to '♡', post_comment_path(@post,comment), name: "promote", class: "button"%><%= comment.Evaluation %> <% end %> <% end %> </div> <% end %> </ul> <% end %> <%= form_for([@post, @post.comments.build]) do |f| %> <p> <%= f.text_field :body, class: "show-content" %> </p> <p> <%= f.submit "回答する", class: "comment" %> </p> <% end %> </div> </div> </div> </div> </div>
class CommentsController < ApplicationController def create @post = Post.find(params[:post_id]) @post.comments.create(comment_params.merge(user_id: current_user.id)) redirect_to post_path(@post) end def destroy @post = Post.find(params[:post_id]) @comment = @post.comments.find(params[:id]) @comment.destroy redirect_to post_path(@post) end def update if params[:promote] then @post.comments.increment!(:Evaluation, by = 0) @post.comments.touch redirect_to post_comment_path(@post,@comment) end end private def comment_params params.require(:comment).permit(:body) end # Use callbacks to share common setup or constraints between actions. def set_comment @post.comments = Comment.find(params[:id]) end end

rails routes

Prefix Verb URI Pattern users#destroy activate_user GET /users/:id/activate(.:format) users#activate GET /users(.:format) users#index POST /users(.:format) users#create new_user GET /users/new(.:format) users#new edit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy users#index GET /users/new(.:format) users#new GET /users/:id(.:format) users#show root GET / posts#index GET /users(.:format) users#index POST /users(.:format) users#create GET /users/new(.:format) users#new GET /users/:id/edit(.:format) users#edit GET /users/:id(.:format) users#show PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy post_comment_likes POST /posts/:post_id/comments/:comment_id/likes(.:format) likes#create post_comment_like DELETE /posts/:post_id/comments/:comment_id/likes/:id(.:format) likes#destroy post_comments GET /posts/:post_id/comments(.:format) comments#index POST /posts/:post_id/comments(.:format) comments#create new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit post_comment GET /posts/:post_id/comments/:id(.:format) comments#show PATCH /posts/:post_id/comments/:id(.:format) comments#update PUT /posts/:post_id/comments/:id(.:format) comments#update DELETE /posts/:post_id/comments/:id(.:format) comments#destroy posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PATCH /posts/:id(.:format) posts#update PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy genres GET /genres(.:format)

以上です。

お力お貸しください。

どうぞよろしくお願いいたします。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問