2012-11-14 50 views
3

我試圖更新我的關聯模型,但它並未更新到數據庫,我不確定接下來要嘗試什麼。Rails相關模型在數據庫中未更新

型號

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 

控制器

# 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 

查看

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

回答

3

attr_accessible應該接受user_details_attributes,不只是user_details

+0

哇這麼簡單,但我永遠不會那麼做。 – Catfish