2013-11-01 34 views
0

我有一個用戶模型has_one關係到一個配置文件模型。表單生成正確(我看到contact_person的附加字段),但創建新記錄時只保存用戶。我錯過了什麼?嵌套has_one關係不保存的表格

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 

    has_one :profile 
    accepts_nested_attributes_for :profile 
end 

class Profile < ActiveRecord::Base 
    belongs_to :user 
end 

這是我的形式:

<% @user.build_profile if @user.profile.nil? %> 
<%= f.fields_for :profile do |profile| %> 
    <%= profile.label :contact_person %> 
    <%= profile.text_field :contact_person %> 
<% end %> 

回答

1

您必須添加attr_accessible:在用戶模式profile_attributes。

class User < ActiveRecord::Base 

    attr_accessible :profile_attributes 

     devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

     has_one :profile 
     accepts_nested_attributes_for :profile 
    end 
+0

我正在使用Rails 4,並且已棄用attr_accessible。 –