2012-11-14 51 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