title: Getting fields_for and accepts_nested_attributes_for to work with a belongs_to relationship
body: <p>I cannot seem to get a nested form to generate in a rails view for a <code>belongs_to</code> relationship using the new <code>accepts_nested_attributes_for</code> facility of Rails 2.3. I did check out many of the resources available and it looks like my code <em>should</em> be working, but <code>fields_for</code> explodes on me, and I suspect that it has something to do with how I have the nested models configured.</p>
  
  <p>The error I hit is a common one that can have many causes:</p>
  
  <pre><code>'@account[owner]' is not allowed as an instance variable name
  </code></pre>
  
  <p>Here are the two models involved:</p>
  
  <pre><code>class Account &lt; ActiveRecord::Base
    # Relationships
    belongs_to :owner, :class_name =&gt; 'User', :foreign_key =&gt; 'owner_id'
    accepts_nested_attributes_for :owner
    has_many :users
  end
  
  class User &lt; ActiveRecord::Base
    belongs_to :account
  end
  </code></pre>
  
  <p>Perhaps this is where I am doing it 'rong', as an Account can have an 'owner', and may 'users', but a user only has one 'account', based on the user model account_id key.</p>
  
  <p>This is the view code in new.html.haml that blows up on me:</p>
  
  <pre><code>- form_for :account, :url =&gt; account_path do |account|
    = account.text_field :name
    - account.fields_for :owner do |owner|
      = owner.text_field :name
  </code></pre>
  
  <p>And this is the controller code for the new action:</p>
  
  <pre><code>class AccountsController &lt; ApplicationController
    # GET /account/new
    def new
      @account  = Account.new
    end
  end
  </code></pre>
  
  <p>When I try to load /account/new I get the following exception:</p>
  
  <pre><code>NameError in Accounts#new
  Showing app/views/accounts/new.html.haml where line #63 raised:
  @account[owner] is not allowed as an instance variable name
  </code></pre>
  
  <p>If I try to use the mysterious 'build' method, it just bombs out in the controller, perhaps because build is just for multi-record relationships:</p>
  
  <pre><code>class AccountsController &lt; ApplicationController
    # GET /account/new
    def new
      @account  = Account.new
      @account.owner.build
    end
  end
  
  You have a nil object when you didn't expect it!
  The error occurred while evaluating nil.build
  </code></pre>
  
  <p>If I try to set this up using @account.owner_attributes = {} in the controller, or @account.owner = User.new, I'm back to the original error, "@account[owner] is not allowed as an instance variable name".</p>
  
  <p>Does anybody else have the new accepts_nested_attributes_for method working with a belongs_to relationship? Is there something special or different you have to do? All the official examples and sample code (like the <a href="http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes" rel="noreferrer">great stuff over at Ryans Scraps</a>) is concerned with multi-record associations.</p>