title: Defining users within partials
body: <p>I am new to ruby on rails, and know this is a beginner error.  I'm trying to create an application that displays user comments within a post.  I have a comment partial pointing to my post#show, and when requesting to display &lt;%= image_tag(comment.user.avatar.small.url, class: "media-object") if comment.user.avatar? %> from _comment.html.erb, I get "undefined method 'user' for nil:NilClass.  I've read other questions on stackoverflow  and I know this is saying it can't find a user associated with the post within comments and that I need to define user in my controller.  I'm assuming it's for the post controller, but I can't seem to wrap my head around the specific syntax to display comments by user in post.  </p>
  
  <pre><code>#comments_controller
  def create
   @topic = Topic.find(params[:topic_id])
   @post = Post.find(params[:post_id])
   @comment = current_user.comments.new(comment_params)
   @comment.post = @post
   @comment.save
   authorize @comment
   redirect_to [@topic, @post]
  end
  
  #posts_controller
  def show
   @topic = Topic.find(params[:topic_id])
   @post = Post.find(params[:id]) 
  end
  
  #post model
  class Post &lt; ActiveRecord::Base
   belongs_to :user
   belongs_to :topic
   has_one :summary
   has_many :comments
   mount_uploader :image, ImageUploader
  
  #comment model
   class Comment &lt; ActiveRecord::Base
    belongs_to :post
    belongs_to :user
  
  #rendering comment partial in post#show
   &lt;h1&gt;&lt;%= @post.markdown_title %&gt;&lt;/h1&gt;
   &lt;div class="row"&gt;
    &lt;div class="col-md-8"&gt;
     &lt;small&gt;
      &lt;%= image_tag(@post.image.thumb.url) if @post.image? %&gt;&lt;/br&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;%= @post.markdown_body %&gt;&lt;/p&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 class="col-md-8"&gt;
   &lt;% if policy(Comment.new).create? %&gt;
     &lt;%= render partial: 'comments/comment', locals: { comment: @comment } %&gt;
     &lt;%= render partial: 'comments/form', locals: {topic: @topic, post: @post} %&gt;
  &lt;% else %&gt;
    &lt;h3&gt;Sign up to Bloccit to post comments&lt;/h3&gt;
    &lt;%= link_to "Sign up", new_user_registration_path, class: 'btn btn-primary' %&gt;&lt;/br&gt;&lt;/br&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>
    </p>
  
  <pre><code>#comment partial
   &lt;ul&gt;
   &lt;% @post.comments.each do |c| %&gt;
    &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;li&gt;
    &lt;p&gt;&lt;%= c.body %&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p></p>
  
  <p>Also, when I put @post.user = @user in posts_controller the error changes to "undefined method 'avatar' for nil:NilClass. Thanks for your help! </p>