Ruby on rails 嵌套的窗体有一个关系未保存
我有一个用户模型,它与配置文件模型有一个关系。表单正确生成(我看到联系人的附加字段),但创建新记录时只保存用户。我错过了什么Ruby on rails 嵌套的窗体有一个关系未保存,ruby-on-rails,ruby,devise,has-one,Ruby On Rails,Ruby,Devise,Has One,我有一个用户模型,它与配置文件模型有一个关系。表单正确生成(我看到联系人的附加字段),但创建新记录时只保存用户。我错过了什么 class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_one :profile accepts_nested_attri
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :profile
accepts_nested_attributes_for :profile
end
class用户
及
类配置文件
这是我的表格:
<% @user.build_profile if @user.profile.nil? %>
<%= f.fields_for :profile do |profile| %>
<%= profile.label :contact_person %>
<%= profile.text_field :contact_person %>
<% end %>
您必须在用户模型中添加attr\u accessible:profile\u属性
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
class用户
因此,我在维基上找到了这些信息,解决了我的问题 我使用的是Rails 4,不推荐使用attr_accessible。
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