title: Retain nested form field value after failed validation
body: <p>I am new to rails and have not yet managed to solve the following issue. I use devise for authentication with a model called "user". This model is associated to another model called "company":</p>
  
  <p><strong>user.rb</strong></p>
  
  <pre><code>  class User &lt; ActiveRecord::Base
  
    devise :database_authenticatable, :registerable, :confirmable,
           :recoverable, :rememberable, :trackable, :validatable
  
    belongs_to :company
    accepts_nested_attributes_for :company
  
    attr_accessible :company_attributes, :email, :password, 
                    :password_confirmation, :remember_me, :company_id
  </code></pre>
  
  <p><strong>company.rb</strong></p>
  
  <pre><code>  class Company &lt; ActiveRecord::Base
  
    has_many :users
    attr_accessible :name
    validates :name, :presence =&gt; true, :uniqueness =&gt; true
  </code></pre>
  
  <p>On registration, a user creates both a company and a user:</p>
  
  <p><strong>users_controller.rb</strong></p>
  
  <pre><code>def new
      @user = User.new
      @user.build_company
  
      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @user }
      end
  end
  
  def create
    @user = User.new(params[:user])
  
    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render json: @user, status: :created, location: @user 
      else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
  </code></pre>
  
  <p>The registration works out so far, meaning that if <code>@user.save</code> is true, two records are create each in the user and the company model with the correct association in the column <code>company_id</code> in the user model.</p>
  
  <p>If <code>@user.save</code> is false, for instance because the validation for the user attribute email fails because it has already been taken, a new view is rendered. As expected, the <code>@user</code> attribute, in this case email, is retained and remains in the respective form field after <code>render action: 'new'</code>. The form field for the company attribute name, however, appears blank. This is the view for the registration page:</p>
  
  <p><strong>new.html.erb</strong></p>
  
  <pre><code>&lt;%= devise_error_messages! %&gt;
  &lt;h2&gt;Sign up&lt;/h2&gt;
  &lt;%= simple_form_for(resource, :as =&gt; resource_name, 
                                :url =&gt; registration_path(resource_name),
                                :html =&gt; {:class =&gt; 'form-horizontal' }) do |f| %&gt;
  
      &lt;%= f.simple_fields_for :company,  @user.build_company do |builder| %&gt;
          &lt;div&gt;&lt;%= builder.label :name, "Company" %&gt;&lt;br /&gt;
          &lt;%= builder.text_field :name, :autofocus =&gt; true, 
                              :placeholder =&gt; "Example Company Inc.",
                              :required =&gt; true %&gt;&lt;/div&gt;
      &lt;% end %&gt;
  
    &lt;div&gt;&lt;%= f.label :email %&gt;&lt;br /&gt;
    &lt;%= f.email_field :email, :placeholder =&gt; "example@email.com",  :required =&gt; true %&gt;&lt;/div&gt;
  
    &lt;div&gt;&lt;%= f.label :password %&gt;&lt;br /&gt;
    &lt;%= f.password_field :password,  :required =&gt; true %&gt;&lt;/div&gt;
  
    &lt;div&gt;&lt;%= f.label :password_confirmation %&gt;&lt;br /&gt;
    &lt;%= f.password_field :password_confirmation,  :required =&gt; true %&gt;&lt;/div&gt;
  
    &lt;%= f.submit "Sign up", :class =&gt; "btn"%&gt;
  &lt;% end %&gt;
  
  &lt;%= render "devise/shared/links" %&gt;
  </code></pre>
  
  <p>I thought that I might have to create an instance variable <code>@company</code> in the user create method so that the form field content can be saved within the params hash like this:</p>
  
  <pre><code>@company = @user.build_company(params[:company])
  </code></pre>
  
  <p>Unfortunately, this does not work and so far I have not found a solution in other questions posted on stackoverflow. I am looking forward to your input so that i can proceed building my first app ;)</p>
  
  <p>Thanks in advance for your help.</p>