When you try to delete a comment, the following error is displayed:
ActiveRecord :: RecordNotFound in PostsController # destroy
Couldn't find Post with 'id' = <Comment :: ActiveRecord_Associations_CollectionProxy: 0x00007f67f0090ce0>
Extracted source (around line # 39):
def destroy @post = Post.find (params [: id])
@ post.destroy redirect_to posts_path
My code is:
class CommentsController < ApplicationController def destroy @post.comments= Post.find(params[:post_id]) @post.comments.destroy redirect_to posts_path(@post) end end # routes.rb Rails.application.routes.draw do root 'posts#index', as: 'home' resources :posts do resources :comments end end # posts/show.html.erb <h1><%=@post.title %> </h1> <p><%=@post.body %></p> <p> <%= image_tag @post.image.url(:thumb), class: 'img-show' if @post.image? %> </p> <hr> <%= link_to "Редактировать", edit_post_path(@post), :class=> 'btn btn-warning' %> <%= link_to "Удалить пост", post_path(@post), method: :delete, data: {confirm:"Вы хотите удалить статью ?"}, :class => 'btn btn-danger'%> <h2>Вcе коментарии</h2> <% @post.comments.each do |comments| %> <div class="alert alert-light"> <p><strong><%=comments.username%></strong>: <%=comments.body%></p> </div> <% end %> <h2>Коментарии</h2> <%=form_for([@post, @post.comments.build]) do |f| %> <p> Пользователь<br> <%=f.text_field(:username)%> </p> <p> Текст комментария<br> <%= f.text_area(:body)%> </p> <p> <%= f.submit("Добавить Коментарий")%> </p> <% end %> <%= link_to "Удалить Комент ", post_path(@post.comments), method: :delete, data: {confirm:"Вы хотите удалить?"}, :class => 'btn btn-danger'%>