title: Rails Guide "undefined method 'comments' for nil:NilClass"
body: <p>I am new to Rails and working through the <a href="http://guides.rubyonrails.org/getting_started.html" rel="nofollow noreferrer">Getting Started Guide</a>. I read through <a href="https://stackoverflow.com/questions/17810348/undefined-method-title-for-nilnilclass-rails-guides-tutoraial">this</a> similar question about the guide but it doesn't seem relevant.</p>
  
  <p>I am stuck on section 6.3, where we are trying to let users add comments to blog posts. I have added a comments form to the post show view, which was working fine before, but now raises the following error. What is the issue?</p>
  
  <pre><code>NoMethodError in Posts#show
  
  Showing /Users/.../Desktop/Rails Blog/blog/app/views/posts/show.html.erb where line #24 raised: 
  
  undefined method `comments' for nil:NilClass
  
  
  
  Extracted source (around line #24):
  
      21 &lt;% end %&gt;
      22 
      23 &lt;h2&gt;Add a comment:&lt;/h2&gt;
      24 &lt;%= form_for([@post, @posts.comments.build]) do |f| %&gt;
      25    &lt;p&gt;
      26         &lt;%= f.label :commenter %&gt;&lt;br /&gt;
      27         &lt;%= f.text_field :commenter %&gt;
  </code></pre>
  
  <p><strong>posts_controller.rb:</strong></p>
  
  <pre><code>class PostsController &lt; ApplicationController
      def index
          @posts = Post.all
      end
      def new
          @post = Post.new
      end
      def create
          @post = Post.new(params[:post].permit(:title, :text))
          if @post.save
              redirect_to @post
          else
              render 'new'
          end
      end
      def edit
          @post = Post.find(params[:id])
      end
      def update
          @post = Post.find(params[:id])
  
          if @post.update(params[:post].permit(:title, :text))
              redirect_to @post
          else
              render 'edit'
          end
      end
      def show
          @post = Post.find(params[:id])
      end
      def destroy
          @post = Post.find(params[:id])
          @post.destroy
          redirect_to posts_path
      end
      private
          def post_params
              params.require(:post).permit(:title, :text)
          end
  end
  </code></pre>
  
  <p><strong>comments_controller:</strong></p>
  
  <pre><code>class CommentsController &lt; ApplicationController
      def create
          @post = Post.find(params[:post_id])
          @comment = @post.comments.create(params[:comment].permit(:commenter, :body))
          redirect_to post
      end
  end
  </code></pre>
  
  <p><strong>posts.show.html.erb:</strong></p>
  
  <pre><code>&lt;p&gt;
      &lt;strong&gt;Title:&lt;/strong&gt;
      &lt;%= @post.title %&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
      &lt;strong&gt;Text:&lt;/strong&gt;
      &lt;%= @post.text %&gt;
  &lt;/p&gt;
  
  &lt;h2&gt;Comments&lt;/h2&gt;
  &lt;% @post.comments.each do |comment| %&gt;
      &lt;p&gt;
          &lt;strong&gt;Commenter:&lt;/strong&gt;
          &lt;%= comment.commenter %&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;strong&gt;Comment:&lt;/strong&gt;
          &lt;%= comment.body %&gt;
      &lt;/p&gt;
  &lt;% end %&gt;
  
  &lt;h2&gt;Add a comment:&lt;/h2&gt;
  &lt;%= form_for([@post, @posts.comments.build]) do |f| %&gt;
      &lt;p&gt;
          &lt;%= f.label :commenter %&gt;&lt;br /&gt;
          &lt;%= f.text_field :commenter %&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;%= f.label :body %&gt;&lt;br /&gt;
          &lt;%= f.text_area :body %&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;%= f.submit %&gt;
      &lt;/p&gt;
  &lt;% end %&gt;
  
  &lt;%= link_to 'Back', posts_path %&gt;
  &lt;%= link_to 'Edit', edit_post_path(@post) %&gt;
  </code></pre>