Undefined method, avatar and name for nil:NilClass

undefined method avatar?' for nil:NilClass undefined methodname' for nil:NilClass

Hi, I'm receiving the following errors in my partial. The reason I listed both is because after commenting out the line causing the first error message, I get the second error which leads me to believe the problem isn't with "avatar" or "name" specifically, but with something else,though I don't know what. In rails console, I'm able to call user and name on a comment. I also seeded the database using Faker if that matters. Here's the partial.

<%= 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 %>

Also, please see the render.

  <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>

The below are my user model and 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

I've already reset the database, but to no avail. Stuck as to what the issue is. Thanks for your help.

Please see below for my User and Comment models.

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

User model

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


ANSWERS:


If you're getting

undefined method foo for nil:NilClass

it's that the thing you're calling your method on is nil.

So in your case, you're calling avatar? and name on something nil.

Looking at your code, it's clear comment.user is (a) what those methods are called on, and hence (b) what is nil.

Result: your comment has no user. Either enforce all comments (including new/empty/stub ones) to have an user (blank user?), or make your view so that a user is not necessary.


The issue was discovered. In the partial render

comment: @comment

should be

comment: comment


 MORE:


 ? undefined method `premium?' for nil:NilClass
 ? undefined method `default_image' for nil:NilClass
 ? C# WinForms - Determine when void method returns
 ? C# - Unable to add item to listview from method invoked from class
 ? GCC : Return statement from a void function in C
 ? How to call objective c method from c method iOS
 ? Creating multiple choice for user
 ? Does "return;" at the end of a void method in Java have ANY impact on performance?
 ? Java If else nested. public static void ()
 ? FETCH API return undefined