簡體   English   中英

Rails 4破壞了操作,導致注釋不起作用

[英]Rails 4 Destroy Action For Comments Not Working

我的評論破壞行動無效。 這是我的鏈接:

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

這是我的銷毀動作:

def destroy
    @article.comments.destroy
    respond_to do |format|
      format.html { redirect_to article_path(@article), notice: 'Comment was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

彈出框確實出現了,讓我點擊確定。 閃爍表示已成功,但未刪除注釋。 任何幫助表示贊賞!

您沒有向我們展示@article的填充方式,所以我們假裝它不可用並重新開始。 首先,您需要找到父Article 文章的ID應該在params中作為article_id 接下來,您需要使用文章的comments集合,並使用ID = params[:id]銷毀該注釋。

def destroy
  article = Article.find(params[:article_id])
  article.comments.find(params[:id]).destroy

  respond_to do |format|
    format.html { redirect_to article_path(article), notice: 'Comment was successfully destroyed.' }
    format.json { head :no_content }
  end
end

從技術上講,您不需要通過comments關聯銷毀評論,但這始終是一種好習慣。

首先,您要在整個@article.comments集合上調用destroy,而不是在單個注釋上。

其次,您將需要為請求/響應粘貼日志輸出,以提供更多幫助。 我的猜測是您拋出錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM