0

不知道为什么我在开发中收到此错误消息,因为它只是在一分钟前工作,并且仍在生产中工作。

所有关联都完好无损,不知道这个消息是从哪里来的?发生了什么?

这是错误消息:

NoMethodError in Comments#index
Showing /Users/apane/Downloads/leap_stage/leap_stage/app/views/comments/index.html.erb

undefined method `title' for nil:NilClass

   <span class="title">
   <li>comment by: <strong><%= comment.author_name %></strong></span>

   <%= time_ago_in_words(comment.created_at) + " ago" %> | on <%= link_to comment.song.title, comment.song %></span>
    <br />
    <%= comment.content %><br></li>
    <hr>

评论#index.html.erb

<div id="layout-1">
<!--div class="left-side"> -->
<%#= will_paginate @songs %>
<h6>Latest comments</h6>
<hr>
<ul><% @comments.each do |comment| %>
<span class="title">
<li>comment by: <strong><%= comment.author_name %></strong></span>

<%= time_ago_in_words(comment.created_at) + " ago" %> | on <%= link_to comment.song.title, comment.song %></span>
    <br />
 <%= comment.content %><br></li>
    <hr>


 <%#=link_to '&#9660'.html_safe, vote_against_song_path(song), :remote => true, :method => :put %> 

<%#= link_to 'Show', song, class: "button small secondary" %>
<%= link_to('Edit', edit_comment_path(comment), class: "button small secondary") if can? :update, comment %>
<%= link_to('Destroy', comment, method: :delete, data: {confirm: 'Are you sure?'}, class: "button small secondary") if can? :destroy, comment %>

<% end %>

</ol>
</div>

评论控制器.rb

class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]


  def index
    @comments = Comment.all
  end

  def show
  end

  # GET /comments/new
  def new
  end

  # GET /comments/1/edit
  def edit
  end

  # POST /comments
  # POST /comments.json
  def create
    @comment = Comment.new(comment_params)

    respond_to do |format|
      if @comment.save
        format.html { redirect_to song_url(@comment.song_id), notice: 'Comment was successfully created.'  }
        format.json { render action: 'show', status: :created, location: @comment}
      else
        format.html { render action: 'new' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end


  # PATCH/PUT /comments/1
  # PATCH/PUT /comments/1.json
  def update
    respond_to do |format|
      if @comment.update(comment_params)
        format.html { redirect_to song_url(@comment.song_id), notice: 'Comment was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.json
  def destroy
    @comment.destroy
      redirect_to song_url(@comment.song_id)
    end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_comment
      @comment = Comment.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def comment_params
      params.require(:comment).permit(:song_id, :author_name, :site_url, :content, :user_id)
    end
end
4

2 回答 2

1

您收到unidentified method错误是因为 - 无论出于何种原因 - 您尝试迭代的一个(或多个)评论与父歌曲无关

虽然您无法在视图中真正捕捉到此错误,但您可以通过仅访问与歌曲关联的评论的歌曲属性来防止它:

# app/views/comments/index.html.erb
<% @comments.each do |comment| %>
    <% if comment.song %>
        # comment/song logic
    <% end %>
<% end %>

通过确保仅当comment.song is not 时才执行视图逻辑,您可以规避与在对象nil上查找属性相关的错误。NilClass

或者,在您的控制器中,您可以只查询属于父歌曲的评论:

# app/controllers/comments_controller.rb
def index
    @comments = Comment.where("song_id IS NOT ?", nil) # returns only comments belonging to a parent song
end
于 2013-07-29T01:36:35.047 回答
0

我敢打赌你的一个comment记录没有song

<%= link_to comment.song.title, comment.song %><
于 2013-07-29T01:33:49.077 回答