title: Rails undefined method `build' for nil:NilClass
body: <p>I'm trying to created a form that accepts nested attributes from another model. But in the new function in the controller I'm running @item.item_type.build and get this error</p>
  
  <pre><code>undefined method `build' for nil:NilClass
  </code></pre>
  
  <p>this is the new function in the items_controller</p>
  
  <pre><code>  def new
      @item = Item.new
      @item_gallery = @item.item_galleries.build
      @item_type = @item.item_type.build
    end
  </code></pre>
  
  <p>Params:</p>
  
  <pre><code>def item_params
    params.require(:item).permit(:title, :price, :description, item_galleries_attributes: [:id, :item_id, :image], item_type_attributes: [:id, :type, :item_id])
  end
  </code></pre>
  
  <p>and inside the item.rb (model) file:</p>
  
  <pre><code>  has_many :item_galleries, dependent: :destroy
    has_one :item_type
    accepts_nested_attributes_for :item_galleries
    accepts_nested_attributes_for :item_type
  </code></pre>
  
  <p>I'm basically trying to set the item type from a form dropdown.</p>
  
  <p>Example:</p>
  
  <pre><code>&lt;%= f.fields_for :item_types do |t| %&gt;
    &lt;%= t.label :type %&gt;
    &lt;%= t.select :type, options_for_select(["Type1", "Type2", "Type3"]), prompt: "Select One" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>The idea is to eventually filter the items based on item_type</p>