Issues when updating nested attributes in rails

Hi,

I am having some issues when updating an object that has a nested
attribute.

My model objects are:

User

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

I have got the action update defined in the User controller like
this:

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

The update action for the PortalUser controller is also defined:

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

And finally I have a Profile controller that hosts the nested form:

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

View of the Profile’s action 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 %>

When I go to the edit page, I can see the information of the user
(both @user and @portal_user) loaded into the form but when I edit the
form and send the update nothing happens!

To help you discover the origin of my problem here is the ‘trace’ (in
this I tried to rename to change the username field from Amokrane to
amk:

Started POST "/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)

It could be something obvious given I am relatively new to rails!

Thank you!

Ok! I have just found where the problem was! I had some additional
validations written in my PortalUser model and it was updating because
I wasn’t filling all the necessary fields.