验证错误后未嵌套的嵌套模型数据

时间:2012-01-16 21:38:03

标签: ruby-on-rails nested-forms

我有一个嵌套的表单 - person / user,1:1 - 用户表单是外部表单。

每次出现验证错误并且用户被重定向回页面时,都不会加载嵌套人员属性。

如果没有出现验证错误,一切都会正确保存。

此时我正在搜索空白并尝试修复此问题。

我尝试过两种方式都设置了“has_one / belongs_to”关系,似乎没有任何区别。在控制器中,我尝试了尽可能多的设置数据的变体 - 有些导致没有保存,但没有一个允许我在出错时返回填充的人物对象。

感谢任何帮助。

Rails版本:3.1.3

主要形式:

<%= 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 %>

嵌套表单,加上更多主要表单:

<%= 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>

用户:

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

人:

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

当页面呈现时,我已经对@user和@ user.person进行了检查并获得以下内容:

@user:

{"challenge_question"=>"", "encrypted_password"=>"", "created_at"=>nil, "failed_attempts"=>0, "updated_at"=>nil, "confirmation_sent_at"=>nil, "last_sign_in_ip"=>nil, "role"=>1, "last_sign_in_at"=>nil, "sign_in_count"=>0, "id"=>nil, "current_sign_in_ip"=>nil, "reset_password_token"=>nil, "person_id"=>nil, "challenge_answer"=>"", "disabled"=>false, "locked_at"=>nil, "confirmation_token"=>nil, "current_sign_in_at"=>nil, "remember_created_at"=>nil, "reset_password_sent_at"=>nil, "login"=>"asdf", "unlock_token"=>nil, "confirmed_at"=>nil, "email"=>""}

@ user.person:

{"cc_city"=>nil, "token"=>nil, "last"=>"", "id"=>nil, "cc_token"=>nil, "address_id"=>nil, "phone"=>"", "first"=>"asdf", "email"=>""} 

1 个答案:

答案 0 :(得分:0)

不完全确定为什么会这样,但这似乎解决了这个问题:

自:

<%= f.fields_for(:person) do |person_form| %>

要:

<%= f.fields_for(:person, @user.person) do |person_form| %>

文件似乎表明前者应该有效。我还没有挖掘底层代码,看看为什么这不起作用。

相关问题