2013-04-21 50 views
1

我有一个Developer模型:has_oneUser模型。这允许跨不同用户类型的身份验证和内容。嵌套模型验证错误显示在创造而不是更新

当我创建一个不正确的用户数据的新开发者,它呈现验证错误的列表。当我用不正确的用户数据更新开发人员时,它只是重新呈现编辑表单(因为它应该),但不显示验证错误。

我的验证错误显示代码坐在我的领域部分的形式,所以,不应该有所作为。

我觉得问题在于我试图更新模型的方式。

def update 
    @developer = Developer.find(params[:id]) 
    if @developer.user.update_attributes(params[:user]) && @developer.update_attributes(params[:developer]) 
    flash[:success] = "Profile Updated" 
    sign_in @developer.user 
    redirect_to @developer 
    else 
    render 'edit' 
    end 
end 

和我User验证没有任何幻想:

validates :name, presence: true, length: {maximum: 30} 
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
validates :email, presence: true, 
       format: { with: VALID_EMAIL_REGEX }, 
       uniqueness: { case_sensitive: false } 
validates :password, length: {minimum: 6} 
validates :password_confirmation, presence: true 

我读过的至少10种不同的发音相似的职位,但我没有发现任何帮助真实。任何帮助都会很棒。

编辑

当我提交我的update形式,以下PARAMS来通过

Parameters: {"utf8"=>"✓", "authenticity_token"=>"70xmNVxxES7lK2bSIIul/i5GaiJhB9+B5bV/bUVFlTs=", "user"=>{"name"=>"foo", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "developer"=>{"skype_name"=>""}, "commit"=>"Save changes", "id"=>"11"} 

它仍然没有做User验证。如果我做的通过控制台下面,它的工作原理虽然(即它保存在PARAMS是好的,失败时PARAMS是坏的):

Developer.last.update_attributes(:user_attributes => {:name => "test updated", :email => "[email protected]", :password => "123456", :password_confirmation => "123456"}) 

所以这似乎有所不同,以我的唯一的事情是:user_attributes相当而不仅仅是我的表单给我的:user。我该如何改变它?

编辑2

的形式我_fields部分的有关部分:

<%= render 'shared/error_messages' %> 

<%= fields_for :user do |user| %> 
    <%= user.label :name %> 
    <%= user.text_field :name %> 

    <%= user.label :email %> 
    <%= user.text_field :email %> 

    <%= user.label :password %> 
    <%= user.password_field :password %> 

    <%= user.label :password_confirmation, "Confirm Password" %> 
    <%= user.password_field :password_confirmation %> 
<% end %> 

和我Developer#edit行动:

def edit 
    @developer = Developer.find(params[:id]) 
end 

回答

1

无需单独保存用户和开发者,您可以设法通过像这样的开发人员模式保存用户,

<%= form_for(@developer) do |f| %> 

     ... developer's attribute ... 

      <%= f.fields_for :user do |ff| %> 
      ... user's attribute ... 

控制器中,只有

@developer = Developer.find(params[:id]) 
    if @developer.update_attributes(params[:developer]) 
     .... 

在开发模式,你只需要添加,

 accepts_nested_attributes_for :user 

 attr_accessible :user_attribute 

现在的form_for将自动显示验证用户模型的错误也是如此。

看到这个链接的更多细节http://rubysource.com/complex-rails-forms-with-nested-attributes/

+0

好酷。我曾尝试过'@ developer.update_attributes(params [:developer])',但它说了一些有关对受保护属性或类似内容进行大规模分配的内容。我有'accept_nested_attributes_for:user',但缺少'attr_accessible:user_attribute'。我会稍后再尝试,如果它正常工作,请将其标记为正确:) – 2013-04-22 09:45:50

+0

我尝试在'attr_accessible:user_attribute'中添加,但没有任何帮助。如果我调用'@ developer.update_attributes(params [:developer])',那么肯定只更新了开发者属性而不是用户属性?我怎样才能做到这一点? – 2013-04-23 10:33:09

+0

确保params [:developer] [:user_attributes]不是零。 – 2013-04-23 14:46:49