【发布时间】:2014-05-05 07:09:23
【问题描述】:
nil:NilClass 的未定义方法 avatar?' for nil:NilClass
undefined methodname'
您好,我收到以下部分错误。我列出两者的原因是因为在注释掉导致第一条错误消息的行之后,我得到第二个错误,这使我相信问题不在于“头像”或“名称”,而是与其他东西有关,尽管我不知道是什么。在 Rails 控制台中,我可以在评论中调用用户和名称。如果这很重要,我还使用 Faker 为数据库播种。这里是部分。
<%= content_tag :div, class: 'media', id: "comment-#{comment.id}" do %>
<%= link_to '#', class: 'pull-left' do %>
<%= image_tag(comment.user.avatar.small.url) if comment.user.avatar? %>
<% end %>
<div class="media-body">
<small>
<%= comment.user.name %> commented <%= time_ago_in_words(comment.created_at) %> ago
<% if policy(comment).destroy? %>
| <%= link_to "Delete", [@topic, @post, comment], method: :delete %>
<% end %>
</small>
<p><%= comment.body %></p>
</div>
<% end %>
另外,请看渲染图。
<div class="col-md-4">
<% if policy(Comment.new).create? %>
<h4>Leave a comment</h4>
<br/>
<%= render partial: 'comments/comment', locals: { topic: @topic, post: @post, comment: @comment } %>
<% end %>
</div>
以下是我的用户模型和 cmets_controller
class UsersController < ApplicationController
before_filter :authenticate_user!
def update
if current_user.update_attributes(user_params)
flash[:notice] = "User information updated"
redirect_to edit_user_registration_path(current_user)
else
render "devise/registrations/edit"
end
end
private
def user_params
params.require(:user).permit(:name, :avatar)
end
end
评论控制器
def create
@topic = Topic.find(params[:topic_id])
@post = @topic.posts.find(params[:post_id])
@comments = @post.comments
@comment = current_user.comments.build(comment_params)
@comment.post = @post
@new_comment = Comment.new
authorize @comment
if @comment.save
redirect_to [@topic, @post], notice: "Comment was submitted successfully."
else
flash[:error] = "There was an error submitting the comment. Please try again."
end
end
我已经重置了数据库,但无济于事。坚持到底是什么问题。感谢您的帮助。
请参阅下面的用户和评论模型。
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
default_scope { order('created_at DESC') }
validates :body, length: { minimum: 5 }, presence: true
after_create :send_favorite_emails
private
def send_favorite_emails
self.post.favorites.each do |favorite|
if favorite.user_id != self.user_id && favorite.user.email_favorites?
FavoriteMailer.new_comment(favorite.user, self.post, self).deliver
end
end
end
end
用户模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :posts
has_many :comments
has_many :votes, dependent: :destroy
has_many :favorites, dependent: :destroy
mount_uploader :avatar, AvatarUploader
def role?(base_role)
role == base_role.to_s
end
def favorited(post)
self.favorites.where(post_id: post.id).first
end
def voted(post)
self.votes.where(post_id: post.id).first
end
private
end
【问题讨论】:
-
nil:NilClass错误通常是因为您正在调用未处理的变量或您正在使用的 var 内部包含nil数据 -
您能告诉我们您的
User和Comment模型吗? -
用户未设置评论就是它所说的。为什么?那是上游的某个地方。 current_user.cmets.build(comment_params) 周围是我要看的地方。
-
等等,如果您在控制器中设置
@comment,您的视图中如何设置comment?你不应该使用@comment吗? -
@RichPeck - 我添加了评论和用户模型。我怀疑问题出在调用的方法上,但就像我在上面写的那样,在 rails 控制台中,所有 cmets 都有一个 user_id。另外,我是新手,不知道还有什么好找的
标签: ruby-on-rails methods undefined null avatar