Nested model data not poplating after validation errors
I have a nested form - person/user, 1:1 - with the user form being the outer form.

Each time a validation error arises and the user is redirected back to the page the nested person attributes are not loaded.

Everything saves down correctly if no validation errors arise. 

At this point I'm drawing blanks in my searches and my attempts to fix this. 

I've tried putting the "has_one/belongs_to" relationship both ways and neither seems to make a difference. In the controller I've tried as many variations of setting the data as I can think of - some result in no saving but none have allowed me to return a populated person object on error.

Any help is appreciated.

Rails version: 3.1.3

Main form:

    <%= form_for :user, :url => { :action => :create } do |f| -%>
        …    
        <tr>
            <td><%= f.label :login %></td>
            <td><%= f.text_field :login %></td>
        </tr>    
        ...
        <%= render :partial => '/users/form', :locals => { :f => f } %>
        ...        
        <%= submit_tag 'Register' %>    
    <% end %>

Nested form, plus more main form:

    <%= f.fields_for(:person) do |person_form| %>
        <tr>
            <td><%= person_form.label :first %></td>
            <td><%= person_form.text_field :first %></td>
        </tr>
        <tr>
            <td><%= person_form.label :last %></td>
            <td><%= person_form.text_field :last %></td>
        </tr>
        <tr>
            <td><%= person_form.label :email %></td>
            <td><%= person_form.text_field :email %></td>
        </tr>
        <tr>
            <td><%= person_form.label :phone %></td>
            <td><%= person_form.text_field :phone %></td>
        </tr>
    <% end %>
    
    <tr>
        <td><%= f.label :challenge_question %></td>
        <td><%= f.text_field :challenge_question %></td>
    </tr>
    
    <tr>
        <td><%= f.label :challenge_answer %></td>
        <td><%= f.text_field :challenge_answer %></td>
    </tr>

User:

    class User < ActiveRecord::Base
        has_one :person
    
        devise :database_authenticatable, :confirmable, :recoverable, :registerable, :rememberable, :trackable, :lockable
    
        attr_accessible :login, :password, :password_confirmation, :remember_me, :challenge_question, :challenge_answer, :person_attributes
        accepts_nested_attributes_for :person
    
        validates_presence_of :login
        validates_length_of :challenge_question, :maximum => 400
        validates_length_of :challenge_answer, :maximum => 400
    
        ...
    end

Person:

    class Person < ActiveRecord::Base
        belongs_to :user
    
        validates_presence_of :email, :first, :last
        validates_uniqueness_of :email
        validates_length_of :email, :within => 3..200
        validates_length_of :first, :maximum => 200
        validates_length_of :last, :maximum => 200
        validates_length_of :phone, :maximum => 20, :allow_nil => true
        validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "is not a valid email address."
    
        # prevents a user from submitting a crafted form that bypasses activation
        # anything else you want your user to change should be added here.
        attr_accessible :email, :first, :last, :phone, :token, :cc_token
    end


Nested model data not poplating after validation errors
===

I have a nested form - person/user, 1:1 - with the user form being the outer form.

Each time a validation error arises and the user is redirected back to the page the nested person attributes are not loaded.

Everything saves down correctly if no validation errors arise. 

At this point I'm drawing blanks in my searches and my attempts to fix this. 

I've tried putting the "has_one/belongs_to" relationship both ways and neither seems to make a difference. In the controller I've tried as many variations of setting the data as I can think of - some result in no saving but none have allowed me to return a populated person object on error.

Any help is appreciated.

Rails version: 3.1.3

Main form:

    <%= form_for :user, :url => { :action => :create } do |f| -%>
        …    
        <tr>
            <td><%= f.label :login %></td>
            <td><%= f.text_field :login %></td>
        </tr>    
        ...
        <%= render :partial => '/users/form', :locals => { :f => f } %>
        ...        
        <%= submit_tag 'Register' %>    
    <% end %>

Nested form, plus more main form:

    <%= f.fields_for(:person) do |person_form| %>
        <tr>
            <td><%= person_form.label :first %></td>
            <td><%= person_form.text_field :first %></td>
        </tr>
        <tr>
            <td><%= person_form.label :last %></td>
            <td><%= person_form.text_field :last %></td>
        </tr>
        <tr>
            <td><%= person_form.label :email %></td>
            <td><%= person_form.text_field :email %></td>
        </tr>
        <tr>
            <td><%= person_form.label :phone %></td>
            <td><%= person_form.text_field :phone %></td>
        </tr>
    <% end %>
    
    <tr>
        <td><%= f.label :challenge_question %></td>
        <td><%= f.text_field :challenge_question %></td>
    </tr>
    
    <tr>
        <td><%= f.label :challenge_answer %></td>
        <td><%= f.text_field :challenge_answer %></td>
    </tr>

User:

    class User < ActiveRecord::Base
        has_one :person
    
        devise :database_authenticatable, :confirmable, :recoverable, :registerable, :rememberable, :trackable, :lockable
    
        attr_accessible :login, :password, :password_confirmation, :remember_me, :challenge_question, :challenge_answer, :person_attributes
        accepts_nested_attributes_for :person
    
        validates_presence_of :login
        validates_length_of :challenge_question, :maximum => 400
        validates_length_of :challenge_answer, :maximum => 400
    
        ...
    end

Person:

    class Person < ActiveRecord::Base
        belongs_to :user
    
        validates_presence_of :email, :first, :last
        validates_uniqueness_of :email
        validates_length_of :email, :within => 3..200
        validates_length_of :first, :maximum => 200
        validates_length_of :last, :maximum => 200
        validates_length_of :phone, :maximum => 20, :allow_nil => true
        validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "is not a valid email address."
    
        # prevents a user from submitting a crafted form that bypasses activation
        # anything else you want your user to change should be added here.
        attr_accessible :email, :first, :last, :phone, :token, :cc_token
    end

