在用户.rb
has_one :profile, :dependent => :destroy
has_many :prints, :dependent => :destroy
accepts_nested_attributes_for :profile
before_create :build_profile
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_attributes
安排有问题吗?
编辑1:
错误是:“无法批量分配受保护的属性:配置文件”
编辑2:
我的 profile.rb 模型:
class Profile < ActiveRecord::Base
attr_accessible :name, :address, :phone
belongs_to :user
end
和我的表格:
<%= form_for("user", :url => user_registration_path) do |f| %>
<%= f.email_field :email, :autofocus => true, :placeholder => 'E-mail Address' %>
<%= f.password_field :password, :placeholder => 'Password' %>
<%= f.password_field :password_confirmation, :placeholder => 'Password Confirmation' %>
<%= f.fields_for :profile do |profile_form| %>
<%= profile_form.text_field :name, :placeholder => 'Name' %>
<%= profile_form.text_field :address, :placeholder => 'Address' %>
<%= profile_form.phone_field :phone, :placeholder => 'Phone (example: 0193284647)' %>
<% end %>
<p><%= f.submit "Sign up", :class=>'btn btn-primary' %> </p>
<% end %>
解决方案(使用@Matt 的答案):
<%= profile_form.text_field :name, :placeholder => 'Name' %>
<%= profile_form.text_field :address, :placeholder => 'Address' %>
<%= profile_form.phone_field :phone, :placeholder => 'Phone (example: 0193284647)'
所以它将使用“accepts_nested_attributes_for :profile”