title: Deletion assignment, undefined method `avatar?' for nil:NilClass
body: <p>I am a new student to Ruby on Rails and am currently working on the Deletion HTTP request for my project, a Reddit clone. So far I have implemented Destroy functionality on Topics and Posts successfully. Now that I am working on the deletion of Comments I have hit an error that I am not sure how to deal with.</p>
  
  <p>Here was the instruction.</p>
  
  <blockquote>
    <p>Finally, we also have to modify the comment-listing view -- the app/views/comments/_comment.html.erb partial called in posts#show. Update the partial to look this:</p>
  </blockquote>
  
  <pre><code>&lt;div class="media"&gt;
    &lt;div class="media-left"&gt;
      &lt;%= image_tag(comment.user.avatar.small.url, class: "media-object") if comment.user.avatar? %&gt;
    &lt;/div&gt;
    &lt;div class="media-body"&gt;
      &lt;small&gt;
        &lt;%= comment.user.name %&gt; commented &lt;%= time_ago_in_words(comment.created_at) %&gt; ago
        &lt;% if policy(comment).destroy? %&gt;
          | &lt;%= link_to "Delete", [@topic, @post, comment], method: :delete %&gt;
        &lt;% end %&gt;
      &lt;/small&gt;
      &lt;p&gt;&lt;%= comment.body %&gt;&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>My _comment.html.erb before the change:</strong></p>
  
  <pre><code>&lt;% @comments.each do |comment| %&gt;
  
    &lt;p&gt;&lt;%= comment.body %&gt;&lt;/p&gt;
    &lt;p&gt;&lt;i&gt;&lt;%= comment.user.name %&gt;&lt;/i&gt;&lt;/p&gt;
  
  &lt;% end %&gt;
  </code></pre>
  
  <p>After modifying the code as instructed I receive this error:</p>
  
  <blockquote>
    <p><strong>NoMethodError at /topics/4/posts/11 undefined method `avatar?' for
    nil:NilClass</strong></p>
  </blockquote>
  
  <p>With this line highlighted:</p>
  
  <pre><code>&lt;%= image_tag(comment.user.avatar.small.url, class: "media-object") if comment.user.avatar? %&gt;
  </code></pre>
  
  <p>Here is my <strong>views/post/show.html.erb:</strong></p>
  
  <pre><code>&lt;h1&gt;&lt;%= markdown_to_html @post.title %&gt;&lt;/h1&gt;
  
  &lt;div class="row"&gt;
    &lt;div class="col-md-8"&gt;
      &lt;small&gt;
        &lt;%= image_tag(@post.user.avatar.tiny.url) if @post.user.avatar? %&gt;
        submitted &lt;%= time_ago_in_words(@post.created_at) %&gt; ago by
        &lt;%= @post.user.name %&gt;
      &lt;/small&gt;
      &lt;p&gt;&lt;%= markdown_to_html @post.body %&gt;&lt;/p&gt;
      &lt;p&gt;&lt;%= image_tag(@post.image.post.url) if @post.image? %&gt;
  
      &lt;h1&gt;Comments&lt;/h1&gt;
      &lt;!-- render the comments loop --&gt;
      &lt;%= render partial: 'comments/comment', locals: { topic: @topic, post: @post, comment: @comment } %&gt;
  
      &lt;!-- render the comments form if user is signed in --&gt;
      &lt;% if policy(@comment).create? %&gt;
        &lt;%= render partial: 'comments/form', locals: { topic: @topic, post: @post, comment: @comment } %&gt;
      &lt;% end %&gt;
  
    &lt;/div&gt;
    &lt;div class="col-md-4"&gt;
      &lt;% if policy(@post).edit? %&gt;
        &lt;%= link_to "Edit", edit_topic_post_path(@topic, @post), class: 'btn btn-success' %&gt;
      &lt;% end %&gt;
      &lt;% if policy(@post).destroy? %&gt;
        &lt;%= link_to "Delete Post", [@topic, @post], method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this post?' } %&gt;
      &lt;% end %&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>comments controller</strong></p>
  
  <pre><code>class CommentsController &lt; ApplicationController
    def create
      # find topic by id
      @topic = Topic.find(params[:topic_id])
      # find post id through topic
      @post = @topic.posts.find(params[:post_id])
      # comments on post
      @comments = @post.comments
  
      @comment = current_user.comments.build(params.require(:comment).permit(:body, :post_id, :avatar))
      @comment.post = @post
  
  
      authorize @comment
      if @comment.save
        flash[:notice] = "Comment was created."
        redirect_to [@topic, @post]
      else
        flash[:error] = "Error saving the comment. Please try again."
        # must render the the page calling the form!!
        render 'posts/show'
      end
    end
  
    def new
  
    end
  
    def destroy
      @topic = Topic.find(params[:topic_id])
      @post = Post.posts.find(params[:post_id])
      @comment = @post.comments.find(params[:id])
  
      authorize @comment
      if @comment.destroy
        flash[:notice] = "Comment was removed."
        redirect_to [@topic, @post]
      else
        flash[:error] = "Comment couldn't be deleted. Try again."
        redirect_to [@topic, @post]
      end
    end
  end
  </code></pre>
  
  <p><strong>user model</strong></p>
  
  <pre><code>class User &lt; 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
  
    # CarrierWave method for attribute functionality
    mount_uploader :avatar, AvatarUploader
  
    # These methods check the role of a user in the database
    def admin?
      role == 'admin'
    end
  
    def moderator?
      role == 'moderator'
    end
  end
  </code></pre>
  
  <p>I have attempted to change my comment.user.avatar.small.url to @comment.user.avatar.small.url based off how the rest of my images were handled in the project. The error still persists.</p>
  
  <p>Best regards.</p>