Rails关联模型未在数据库中更新

时间:2012-11-14 00:35:45

标签: ruby-on-rails ruby ruby-on-rails-3

我正在尝试更新我的相关模型,但它没有更新到数据库,我不知道下一步该尝试什么。

模型

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

1 个答案:

答案 0 :(得分:3)

您的attr_accessible应该接受user_details_attributes,而不仅仅是user_details