未定義的方法avatar?' for nil:NilClass undefined method
名」的零:NilClass未定義的方法,頭像和名稱零:NilClass
嗨,我收到我的部分以下錯誤。我列出兩者的原因是因爲在註釋掉導致第一個錯誤信息的行之後,我得到了第二個錯誤,這導致我相信問題不是與「avatar」或「name」具體相關,而是與其他東西相關,儘管我不知道是什麼。在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>
以下是我的用戶模型和comments_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
Comments_controller
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
'零:NilClass'錯誤通常是因爲你要麼調用一個undelcared變量*或*您在使用VAR有'nil'數據在 –
裏面你能向我們展示你的'User'和'Comment'模型嗎? –
用戶未設置評論就是它的意思。爲什麼?那麼這是上游的某個地方。在current_user.comments.build(comment_params)周圍是我要看的地方。 –