Catfish
Catfish

Reputation: 19294

Rails associated model is not updating in database

I'm trying to update my associated model, but it's not updating to the database and i'm not sure what to try next.

Model

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation,
                  :remember_me, :username, :login, :first_name,
                  :last_name, :home_phone, :cell_phone,
                  :work_phone, :birthday, :home_address,
                  :work_address, :position, :company, :user_details

  has_one :user_details, :dependent => :destroy
  accepts_nested_attributes_for :user_details 
end

class UserDetails < ActiveRecord::Base
  belongs_to :user  
  attr_accessible :home_phone, :position  
end

Controller

# PUT /contacts/1/edit
    # actually updates the users data
    def update_user
      @userProfile = User.find(params[:id])

      respond_to do |format|
        if @userProfile.update_attributes(params[:user]) 
          format.html {
            flash[:success] = "Information updated successfully"
            render :edit
          }
        else 
          format.html {
            flash[:error] = resource.errors.full_messages
            render :edit
          }
        end
      end
    end

View

<%= form_for(@userProfile, :url => {:controller => "my_devise/contacts", :action => "update_user"}, :html => {:class => "form grid_6"}, :method => :put ) do |f| %>
  <%= f.label :username, "Username" %>
    <%= f.text_field :username, :required => "required" %>  
    <%= f.fields_for :user_details do |d| %>
      <%= d.label :home_phone, "Home Phone" %>
      <%= d.text_field :home_phone %>
    <% end %>
  <% end %>
<% end %>

Upvotes: 3

Views: 115

Answers (1)

Ryan Bigg
Ryan Bigg

Reputation: 107728

Your attr_accessible should accept user_details_attributes, not just user_details.

Upvotes: 3

Related Questions