2011-02-13 111 views
1

我在更新具有嵌套属性的对象时遇到了一些问题。在rails中更新嵌套属性时的问题

我的模型对象:

用户

class User < ActiveRecord::Base 
    has_one :portal_user 

    accepts_nested_attributes_for :portal_user 

    validates_presence_of :username 
end 

PortalUser

class PortalUser < ActiveRecord::Base 
    belongs_to :user,:dependent => :destroy, :foreign_key => :user_id 
end 

我有定义的操作update在用户控制器是这样的:

class UsersController < ApplicationController 
    # PUT /users/1 
    # PUT /users/1.xml 
    def update 
    @user = User.find(params[:id]) 

    respond_to do |format| 
     if @user.update_attributes(params[:user]) 
     format.html { redirect_to(@user, :notice => 'User was successfully updated.') } 
     format.xml { head :ok } 
     else 
     flash[:error] = "Error while updating personnal information." 
     format.html { render :action => edit_profiles_path } 
     format.xml { render :xml => @user.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 
end 

用于PortalUser控制器更新动作也被定义:

class PortalUsersController < ApplicationController 

    def update 
    @portal_user = PortalUser.find(params[:id]) 

    respond_to do |format| 
     if @portal_user.update_attributes(params[:portal_user]) 
     format.html { redirect_to(@portal_user, :notice => 'PortalUser was successfully updated.') } 
     format.xml { head :ok } 
     else 
     flash[:error] = "Error while updating PortalUser." 
     format.html { render :action => edit_profiles_path } 
     format.xml { render :xml => @portal_user.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

end 

最后我有一个Profile控制器承载嵌套形式:

class ProfilesController < ApplicationController 
    def edit 
    # Note: I am using a hard coded Id on purpose. 
    @user = User.find(980190962) 
    end 
end 

查看个人资料的操作edit

<%= form_for @user do |f| %> 

      <%= f.fields_for :portal_user do |f_portal_user| %> 
      <%= f_portal_user.label :firstname %> <br/> 
      <%= f_portal_user.text_field :firstname %> <br/> 

      <%= f_portal_user.label :lastname %> <br/> 
      <%= f_portal_user.text_field :lastname %> <br/> 
      <% end %> 

      <%= f.label :username %> <br/> 
      <%= f.text_field :username %><br/> 

      <%= f.fields_for :portal_user do |f_portal_user| %> 
      <%= f_portal_user.label :phone %> <br/> 
      <%= f_portal_user.text_field :phone %><br/> 

      <%= f_portal_user.label :cellular_phone %> <br/> 
      <%= f_portal_user.text_field :cellular_phone %><br/> 
      <% end %> 

      <%= submit_tag "Update" %> 
    <% end %> 

当我进入编辑页面时,我可以看到加载到表单中的用户信息(包括@user和@portal_user),但是当我编辑表单并发送更新时,什么也没有发生!

为了帮助你发现我的问题,这里的原点是“追踪”(在此我试图重新命名为用户名字段从改变AmokraneAMK

Started POST "https://stackoverflow.com/users/980190962" for 127.0.0.1 at 2011-02-13 19:38:05 +0100 
    Processing by UsersController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"UA+dbbmwZKpbNYscIvEsqPFwlBkr7yEok1xpYP3/T6k=", "user"=>{"portal_user_attributes"=>{"firstname"=>"Amokrane", "lastname"=>"Chentir", "id"=>"980190962", "phone"=>"", "cellular_phone"=>"0668002010"}, "username"=>"amk"}, "commit"=>"Update", "id"=>"980190962"} 
    User Load (0.1ms) SELECT `users`.* FROM `users` WHERE (`users`.`id` = 980190962) LIMIT 1 
    SQL (0.1ms) BEGIN 
    PortalUser Load (0.3ms) SELECT `portal_users`.* FROM `portal_users` WHERE (`portal_users`.user_id = 980190962) LIMIT 1 
    SQL (0.2ms) ROLLBACK 
    SQL (0.1ms) BEGIN 
    SQL (0.1ms) ROLLBACK 
DEPRECATION WARNING: Giving a path to render :action is deprecated. Please use render :template instead. (called from realtime at /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/benchmark.rb:309) 
Rendered profiles/edit.html.erb within layouts/application (15.1ms) 
Completed 200 OK in 215ms (Views: 23.5ms | ActiveRecord: 0.9ms) 

这可能是很明显的东西给我是比较新的轨道!

谢谢!

回答

0

好吧!我刚才发现问题出在哪里了!我有一些额外的验证软件写n在我的PortalUser模型中,并没有更新,因为我没有填写所有必要的字段。