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)