如何在rails博客应用中删除评论?

时间:2015-12-26 23:30:23

标签: ruby-on-rails ruby comments blogs

我是rails的新手,我已经按照Jumpstart Labs的Blogger教程创建了一个博客应用程序,我正在尝试实现一项功能,允许登录的用户删除文章评论。 但我一直对这个错误信息感到困惑"在CommentsController#destroy中的ActiveRecord :: RecordNotFound 无法找到带有' id' ="

的文章

这是我迄今为止管理的内容,

这是我的app / controllers / comments_controller.rb

    class CommentsController < ApplicationController
    before_filter :require_login, except: [:create]

    def create
        @comment = Comment.new(comment_params)
        @comment.article_id = params[:article_id]

        @comment.save

        redirect_to article_path(@comment.article)
    end

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

      redirect_to article_path(@article)
    end

    private

    def comment_params
        params.require(:comment).permit(:author_name, :body)
    end
end

和我的/views/articles/show.html.erb

    <h1><%= @article.title %></h1>

<p>
  <b>Posted on <%= @article.created_at.strftime("%B %d %Y") %></b>
</p>
<p>
    <% if @article.image.exists? %>
        <%= image_tag @article.image.url%>
    <% end %>
</p>
<p><%= @article.body %></p>
<%= render partial: 'comments/form' %>
<% if logged_in? %>
    <%= link_to "Edit", edit_article_path(@article) %>
    <%= link_to "delete", article_path(@article), method: :delete%>
<% end %>
<%= link_to "<< Back to Articles List", articles_path %>
<h3>Comments (<%=@article.comments.size %>) </h3>
<%= render partial: 'articles/comment', 
collection: @article.comments %>
<p>
    Tags:
    <% @article.tags.each do |tag| %>
        <%= link_to tag.name, tag_path(tag) %>
    <% end %>
</p>

应用程序/视图/物品/ comment.html.erb

   <div>
      <p><b>Comment by:</b> <%= comment.author_name %></p>
      <p class="comment"><%= comment.body %></p>
      <p>Posted <%= distance_of_time_in_words(comment.article.created_at,     comment.created_at) %> later</p>
      <%= link_to 'Delete Comment',  article_comment(article, comment),   method: :delete, data: { confirm: 'Are you sure?' } %>
   </div>

的routes.rb

    Blogger::Application.routes.draw do
  get 'about', to: 'info#about'

  get 'portfolio', to: 'info#portfolio'

  get 'contact', to: 'info#contact'

    root to: 'articles#index'
    resources :articles do
        resources :comments
    end
    resources :tags
    resources :authors
    resources :author_sessions, only: [ :new, :create, :destroy ]
    get 'login' => 'author_sessions#new'
    get 'logout' => 'author_sessions#destroy'

end

和我的articles_controller

class ArticlesController < ApplicationController
include ArticlesHelper
before_filter :require_login, only: [:new, :create, :edit, :update, :destroy]



def index
    @articles = Article.all
end
def new
    @article = Article.new
end

def create
     @article = Article.new(article_params)
     @article.save
     flash.notice = "Article '#{@article.title}' Created!"
     redirect_to article_path(@article)
end

def show
    @article = Article.find(params[:id])
    @comment = Comment.new
    @comment.article_id = @article.id
end

def edit
    @article = Article.find(params[:id])
end

def update
    @article = Article.find(params[:id])
    @article.update(article_params)
    flash.notice = "Article '#{@article.title}' Updated!"
    redirect_to article_path(@article)
end

def destroy

    @article = Article.find(params[:id])
    @article.destroy
    flash.notice = "Article '#{@article.title}' Deleted!"
    redirect_to articles_path
end

任何意见或建议都非常感谢,我一直在寻找谷歌和堆栈溢出一段时间,但我还没有找到解决方案

2 个答案:

答案 0 :(得分:1)

可以通过在 app / views / articles / _comment.html.erb 中更改此行来修复错误NameError in Articles#show undefined local variable or method article(我相信它有_)

来自

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

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

views / articles / show.html.erb 中,尝试将collection: @article.comments更改为collection: @article.comments.all

答案 1 :(得分:0)

要解决这个特殊错误,您需要允许:id和:article_id params

像这样添加它们

params.require(:comment).permit(:author_name, :body, :id, :article_id)
相关问题