title: Rails nested form for belongs_to
body: <p>I'm new to Rails and have some troubles with creating a nested form. </p>
  
  <p>My models:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    belongs_to :company
    accepts_nested_attributes_for :company, :reject_if =&gt; :all_blank
  end
  
  class Company &lt; ActiveRecord::Base
    has_many :users
  end
  </code></pre>
  
  <p>Now I would like to create a new company from the user sign_up page (I use Devise, BTW) by given only a company name. And have a relation between the new User and new Company.</p>
  
  <p>In the console I can create a company for an existing user like this:</p>
  
  <pre><code>@company = User.first.build_company(:name =&gt; "name of company")
  @company.save
  </code></pre>
  
  <p>That works, but I can't make this happen for a new user.  In my new user sign_up form I tried this (I know its wrong by creating a new User first but I'm trying to get something working here.)</p>
  
  <pre><code>&lt;%= simple_form_for(resource, :as =&gt; resource_name, :html =&gt; { :class =&gt; 'form-horizontal' }, :url =&gt; registration_path(resource_name)) do |f| %&gt;
  &lt;%= f.error_notification %&gt;
  
  &lt;div class="inputs"&gt;
    &lt;% 
      @user = User.new
      company = @user.build_company()
  
    %&gt;
    &lt;% f.fields_for company  do |builder| %&gt;
        &lt;%= builder.input :name, :required =&gt; true, :autofocus =&gt; true %&gt;
    &lt;% end %&gt;      
  
    &lt;%= f.input :email, :required =&gt; true, :autofocus =&gt; true %&gt;
    &lt;%= f.input :password, :required =&gt; true %&gt;
    &lt;%= f.input :password_confirmation, :required =&gt; true %&gt;
  &lt;/div&gt;
  
  &lt;div class="form-actions"&gt;
    &lt;%= f.button :submit, :class =&gt; 'btn-primary', :value =&gt; 'Sign up' %&gt;
  &lt;/div&gt;
  </code></pre>
  
    
  
  <p>I did my best to Google for a solution/ example.  I found some nested 
  form examples but it's just not clear to me how to do this.</p>