title: Ruby on Rails Guide: undefined method `fields_for' for nil:NilClass
body: <p>I am going working through the Rails Guides (<a href="http://guides.rubyonrails.org/getting_started.html),and" rel="nofollow">http://guides.rubyonrails.org/getting_started.html),and</a> am stuck at item 11 "Building a Multi Model Form".</p>
  
  <p>This part of the excercise explains about including form fields for one model inside a form for another...</p>
  
  <p>Error on New/Edit Post: <strong>undefined method `fields_for' for nil:NilClass</strong></p>
  
  <pre><code>Extracted source (around line #2):
  
  1: &lt;% @post.tags.build %&gt;
  2: &lt;%= form.fields_for :tags do | tag_form | %&gt;
  3:   &lt;div class="field"&gt;
  4:     &lt;%= tag_form.label :name, 'Tag: ' %&gt;
  5:     &lt;%= tag_form.text_field :name %&gt;
  </code></pre>
  
  <p>The code is exactly as per the excercise (I even got desperate and copy and pasted the code straight from the sample)</p>
  
  <p>My code is posted below.. I have spent hours comparing this to the code in the guide, and it is exactly the same.. can anyone point me to what might be going wrong?</p>
  
  <p>Many Thanks in advance.</p>
  
  <p>Here is my code for including the partial</p>
  
  <p><strong>models/post.rb:</strong></p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
    attr_accessible :content, :name, :title, :tags_attributes
  
    validates :name, :presence =&gt;true
    validates :title, :presence =&gt;true,
                          :length =&gt; { :minimum =&gt; 5 }
    has_many :comments, :dependent =&gt; :destroy
    has_many :tags
  
    accepts_nested_attributes_for :tags, :allow_destroy =&gt; :true,
      :reject_if =&gt; proc { |attrs| attrs.all? { |k, v| v.blank? } }
  end
  </code></pre>
  
  <p><strong>models/tag.rb:</strong></p>
  
  <pre><code>class Tag &lt; ActiveRecord::Base
    belongs_to :post
    attr_accessible :name
  end
  </code></pre>
  
  <p><strong>posts/_form.html.erb:</strong></p>
  
  <pre><code>&lt;% @post.tags.build %&gt;
  &lt;%= form_for(@post) do |post_form| %&gt;
    &lt;% if @post.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@post.errors.count, "error") %&gt; prohibited this post from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @post.errors.full_messages.each do |msg| %&gt;
          &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;div class="field"&gt;
      &lt;%= post_form.label :name %&gt;&lt;br /&gt;
      &lt;%= post_form.text_field :name %&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= post_form.label :title %&gt;&lt;br /&gt;
      &lt;%= post_form.text_field :title %&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= post_form.label :content %&gt;&lt;br /&gt;
      &lt;%= post_form.text_area :content %&gt;
    &lt;/div&gt;
    &lt;h2&gt;Tags&lt;/h2&gt;
    &lt;%= render :partial =&gt; 'tags/form',
                       :locals =&gt; {:from =&gt; post_form} %&gt;
    &lt;div class="actions"&gt;
      &lt;%= post_form.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p><strong>tags/_form.html.erb:</strong></p>
  
  <pre><code>&lt;%= form.fields_for :tags do | tag_form | %&gt;
    &lt;div class="field"&gt;
      &lt;%= tag_form.label :name, 'Tag: ' %&gt;
      &lt;%= tag_form.text_field :name %&gt;
    &lt;/div&gt;
    &lt;% unless tag_form.object.nil?  || tag_form.object.new_record? %&gt;
      &lt;div class="field"&gt;
        &lt;%= tag_form.label :_destroy, 'Remove: ' %&gt;
        &lt;%= tag_form.check_box :_destroy %&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>