ruby on rails コメント削除機能エラー
コメント削除機能実装中にこのエラーが出ました。
解決できないので教えて頂きたいです。
Rails.application.routes.draw do
devise_for :users
get "login" => "users#login_form"
post "login" => "users#login"
post "logout" => "users#logout"
post "users/:id/update" => "users#update"
get "users/:id/edit" => "users#edit"
post "users/create" => "users#create"
get "singnup" => "users#new"
get "users/index" => "users#index"
get "users/:id" => "users#show", as: :user
get "posts/index" => "posts#index"
get "posts/new" => "posts#new"
get "posts/:id" => "posts#show", as: :post
resources :comments, only: [:create,:destroy]
post "posts/create" => "posts#create"
get "posts/:id/edit" => "posts#edit"
post "posts/:id/update" => "posts#update"
post "posts/:id/destroy" => "posts#destroy"
get "/" => "home#top"
get "about" => "home#about"
end
<div class="main posts-show">
<div class="container">
<div class="posts-show-item">
<div class="post-user-name">
<img src="<%= "/user_images/#{@user.image_name}" %>">
<%= link_to(@user.name, "/users/#{@user.id}") %>
</div>
<p>
<%= @post.content %>
</p>
<div class="post-time">
<%= @post.created_at %>
</div>
<% if @post.user_id == @current_user.id %>
<div class="post-menus">
<%= link_to("編集", "/posts/#{@post.id}/edit") %>
<%= link_to("削除", "/posts/#{@post.id}/destroy", {method: "post"}) %>
</div>
<% end %>
<div class="comment-wrapper border-top mb-10">
<% @comments.each do |c| %>
<div>
<% unless c.user.blank? %>
<img src="<%= "/user_images/#{c.user.image_name}" %>">
<% end %>
<%= c.user.name unless c.user.blank? %>
<br />
<%= c.content %>
<%=link_to "削除", comment_path(c.post.id), method: :delete %>
</div>
<br /><% end %>
<% if user_signed_in? %>
<%= form_with(model: @comment, url: comments_path, method: :post, local: true) do |f| %>
<%= hidden_field_tag :post_id, @post.id %>
<%= f.text_area :content, class: "form-control", rows: 5 %>
<%= button_tag type: "submit", class: "btn btn-success float-right mt-1" do %>
<i class="far fa-comments"></i> コメントする
<% end %>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
class Comment < ApplicationRecord
belongs_to :user
belongs_to :post
end
class CommentsController < ApplicationController
before_action :authenticate_user
def create
logger.debug "*"*100
logger.debug "[comments_controller.rb]"
logger.debug "*"*100
post = Post.find(params[:post_id])
@comment = post.comments.build(comment_params)
@comment.user_id = current_user.id
if @comment.save
logger.debug "saved"
flash[:notice] = "コメントしました"
redirect_to(post)
else
logger.debug "failed"
flash[:notice] = "コメントできませんでした"
redirect_to(post)
end
end
def destroy
comment = Comment.find(params[:id])←ここがエラーになりました
comment.destroy
flash[:notice] = "コメントを削除しました"
redirect_to("/posts/index")
end
private
def comment_params
params.require(:comment).permit(:content)
end
logger.debug "*"*100
end