2014-03-26 37 views
1

未定義的方法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 
+1

'零:NilClass'錯誤通常是因爲你要麼調用一個undelcared變量*或*您在使用VAR有'nil'數據在 –

+0

裏面你能向我們展示你的'User'和'Comment'模型嗎? –

+0

用戶未設置評論就是它的意思。爲什麼?那麼這是上游的某個地方。在current_user.comments.build(comment_params)周圍是我要看的地方。 –

回答

1

如果您收到

未定義的方法foonil:NilClass

它,你在呼喚你的方法就是nil

所以在你的情況下,你打電話avatar?name東西無。看看你的代碼,很明顯comment.user是(a)那些方法被調用,因此(b)什麼是零。

結果:你的評論有無用戶。要麼強制執行所有註釋(包括新/空/存根註釋)以使用戶(空白用戶?),要麼使您的視圖變得不需要用戶。

+0

我添加了評論和用戶模型。我懷疑這個問題是由什麼方法被調用的,但就像我上面寫的那樣,在rails控制檯中,所有的註釋都有一個user_id。此外,我是一個新手,所以我不知道還有什麼要尋找的 – user3465296

+0

@ user3465296我會在你的情況下做的是使用[pry](http://pryrepl.org/)(gem' pry-rails'),並在異常之前在你的視圖中插入一個<%binding.pry%>。 –

0

問題被發現。在局部呈現

comment: @comment 

應該

comment: comment 
相關問題