删除帖子的评论会删除整个帖子

时间:2019-08-01 11:13:06

标签: ruby-on-rails

当我尝试删除帖子评论时,我最终会删除整个帖子。

评论控制器

class CommentsController < ApplicationController

  def create
    @micropost = Micropost.find_by(id: params[:micropost_id])
    @comment = 
   @micropost.comments.create(params[:comment].permit(:name,:body))
    if @comment.save
      flash[:success] = "Comment Posted"
  end
    redirect_to request.referrer
  end

  def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy

   redirect_to request.referrer
  end
end

评论视图

<li>
  <ol class="microposts">
    <% @post.comments.each do |comments| %>
      <li>
        <article class="article-container-full">
          <%=  comments.name%>
          <hr>
         <%= comments.body %>
          <hr>
          <p class="posted-time"> Posted <%= 
           time_ago_in_words(comments.created_at) %> ago</p>
          <p>
          <%= link_to 'Delete', @comment, :method => :delete, data: 
            {confirm: 'Are you sure?'}%>
         </p>
       </article>
       <br>
      </li>
    </ol>
  <% end %>
</li>

评论路线

 micropost_comments GET    
 /microposts/:micropost_id/comments(.:format)          
 comments#index
                    POST   
 /microposts/:micropost_id/comments(.:format)          
 comments#create
 new_micropost_comment GET    
 /microposts/:micropost_id/comments/new(.:format)      comments#new
 edit_micropost_comment GET    
 /microposts/:micropost_id/comments/:id/edit(.:format) 
  comments#edit
  micropost_comment GET    
  /microposts/:micropost_id/comments/:id(.:format)      
  comments#show
                    PATCH  
  /microposts/:micropost_id/comments/:id(.:format)      
  comments#update
                    PUT    
  /microposts/:micropost_id/comments/:id(.:format)      
  comments#update
                    DELETE 
  /microposts/:micropost_id/comments/:id(.:format)      
  comments#destroy

Micropost模型:

class Micropost < ApplicationRecord

  has_many :comments, dependent: :destroy
  belongs_to :user
  validates :user_id, presence: true
  validates :headline, presence: true, length: { maximum: 200 }
  validates :content, presence: true, length: { maximum: 10000 }

end

评论模型:

class Comment < ApplicationRecord
  belongs_to :micropost
end

从Microposts Controller中销毁方法只是为了防万一:

  def destroy
    @micropost.destroy
    flash[:success] = "Post deleted"
    redirect_to request.referrer || current_user
  end

我有一种感觉,我想念一些很小的东西,但我只能看到它。它不仅删除评论,还删除整个帖子以及与之相关的所有其他内容。

3 个答案:

答案 0 :(得分:2)

首先,您尚未在任何地方初始化@comment。这就是为什么您会收到此错误。

另一件事是,您不应在do..end循环中使用comments变量。就这样,迭代评论使其成为奇数。

尝试关注,

<li>
  <ol class="microposts">
    <% @post.comments.each do |comment| %>
      <li>
        <article class="article-container-full">
          <%=  comment.name%>
          <hr>
         <%= comment.body %>
          <hr>
          <p class="posted-time"> Posted <%= 
           time_ago_in_words(comment.created_at) %> ago</p>
          <p>
          <%= link_to "Delete Comment", [@post ,comment], :confirm => "Are you sure?", :method => :delete%>
         </p>
       </article>
       <br>
      </li>
    </ol>
  <% end %>
</li>

答案 1 :(得分:1)

您应该尝试此操作-

<%= link_to 'Delete', micropost_comment_path(@post, comments), :method => :delete, data: {confirm: 'Are you sure?'}%>

Or-

<%= link_to 'Delete', [@post, comments], :method => :delete, data: {confirm: 'Are you sure?'}%>

答案 2 :(得分:1)

我知道您已经有了答案,但是您也可以看看shallow nesting。也许是这样的:

resources :posts do 
  resources :comments, shallow: :true
end

将为您提供以下comments条路线:

    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_comment GET    /comments/:id/edit(.:format)              comments#edit
          comment GET    /comments/:id(.:format)                   comments#show
                  PATCH  /comments/:id(.:format)                   comments#update
                  PUT    /comments/:id(.:format)                   comments#update
                  DELETE /comments/:id(.:format)                   comments#destroy

现在(如您所见),DELETE操作不再需要post_id。这样就可以了(从另一个答案的Krupa Suthar借来):

<%= link_to "Delete", comment, confirm: "Are you sure?", method: :delete %>

在这种情况下,您的原始代码:

<%= link_to 'Delete', @comment, :method => :delete, data: {confirm: 'Are you sure?'} %>

...实际上非常接近,除了您需要comment而不是@comment

在这种情况下,浅层嵌套(以及使用更现代的method: :delete而不是:method => :delete)可为您节省11-22个字符:

<%= link_to 'Delete', [@post, comments], :method => :delete, data: {confirm: 'Are you sure?'} %>
<%= link_to "Delete", [@post ,comment], :confirm => "Are you sure?", :method => :delete %>
<%= link_to 'Delete', @comment, :method => :delete, data: {confirm: 'Are you sure?'} %>
<%= link_to "Delete", comment, confirm: "Are you sure?", method: :delete %>

这表示代码减少了约14-22%。看起来似乎不多,但是在您编程的整个过程中,这些字符加起来了。