我不确定为什么我在开发中收到此错误消息,因为它只是在一分钟前工作,并且仍在生产中。
所有关联都完好无损,不知道这条消息来自哪里?发生了什么事?
以下是错误消息:
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 '▼'.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>
comments_controller.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
答案 0 :(得分:1)
您收到unidentified method
错误,因为 - 无论出于何种原因 - 您尝试在上迭代的一条(或多条)评论与关联不与父歌相关联。
虽然您无法在视图中真正捕获此错误,但您可以通过仅访问与歌曲相关联的评论的歌曲属性来阻止它:
通过确保仅在 或者,在您的控制器中,您可以只查询 以查找属于父歌的的 :# app/views/comments/index.html.erb
<% @comments.each do |comment| %>
<% if comment.song %>
# comment/song logic
<% end %>
<% end %>
comment.song
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
答案 1 :(得分:0)
我敢打赌你的comment
个记录之一没有song
<%= link_to comment.song.title, comment.song %><