未定义的方法"each"用于 nil:注释中的 NilClass 错误



在我的应用程序中是一个博客,您可以在帖子下写评论。我的问题是当我想删除评论显示这个错误。

undefined method `each' for nil:NilClass

如何解决此问题?

comment_controller:

class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]
  
  def create
    @link = Link.find(params[:link_id])
    @comment = @link.comments.new(comment_params)
    @comment.user = current_user
     
    respond_to do |format|
      if @comment.save
        format.html { redirect_to @link, notice: 'comment was successfully create'}
        format.json { render json: @comment, status: :created, location: @comment }
      else
        format.html { render action: 'new' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end
  def destroy
    @comment.destroy
    respond_to do |format|
      format.html {redirect_to comments_url, notice: 'Comment was successfully destroyed.'}
      format.json {head :no_content}
    end
  end
  private
    def set_comment
      @comment = Comment.find(params[:id])
    end
    def comment_params
      params.require(:comment).permit(:link_id, :body, :user_id)
    end
end

在尝试删除评论后,将其记录在终端中:

Started GET "/comments" for ::1 at 2015-02-28 11:52:51 +0330
Processing by CommentsController#index as HTML
  Rendered comments/index.html.erb within layouts/application (2.1ms)
Completed 500 Internal Server Error in 8ms
ActionView::Template::Error (undefined method `each' for nil:NilClass):
    13:   </thead>
    14: 
    15:   <tbody>
    16:     <% @comment.each do |comment| %>
    17:       <tr>
    18:         <td><%= comment.body %></td>
    19:         <td><%= comment.user %></td>
  app/views/comments/index.html.erb:16:in `_app_views_comments_index_html_erb__2236689348233653025_70351465687200'
  Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_source.erb (8.9ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_trace.html.erb (2.9ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_request_and_response.html.erb (0.8ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_web_console.html.erb (0.8ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/template_error.html.erb within rescues/layout (26.2ms)

评论index.html.erb

<p id="notice"><%= notice %></p>
<h1>Listing Comments</h1>
<table>
  <thead>
    <tr>
      <th>Link</th>
      <th>Body</th>
      <th>User</th>
      <th colspan="3"></th>
    </tr>
  </thead>
  <tbody>
    <% @comments.each do |comment| %>
      <tr>
        <td><%= comment.body %></td>
        <td><%= comment.user %></td>
        <td><%= link_to 'Show', comment %></td>
        <td><%= link_to 'Edit', edit_comment_path(comment) %></td>
        <td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>
<br>
<%= link_to 'New Comment', new_comment_path %>

我想,应该是,请交叉检查

<% @comments.each do |comment| %>

控制器代码

class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]
  def index
    @comments = Comment.all
  end
  def destroy
    post = Post.where(id: params[:post_id]).take
    post.comments.find(params[:id]).destroy
    respond_to do |format|
      format.html {redirect_to post_url, notice: 'Comment was successfully destroyed.'}
      format.json {head :no_content}
    end
 end

试试这个

最新更新