0

我有一个用户配置文件表单,用户在用户数据库中创建用户记录后填写该表单(通过用户模型)。在配置文件表单和配置文件字段中,我有一个实际上来自用户表的电子邮件字段。这是我的设置:

class Profile < ActiveRecord::Base
  belongs_to :user
  accepts_nested_attributes_for :user
  ...

class User < ActiveRecord::Base
  has_one :profile
  ...

在个人资料表格中:

<%= form_for @profile do |f| %>
  <%= f.fields_for :user do |l| %>
    <%= l.text_field :email, :placeholder => "email address" %>
  <% end %>
  ...

在我的个人资料->新方法中,我有这个..

profile.build_user

..现在发生的情况是,在创建配置文件时会创建一个全新的用户记录。我想要的是,在创建配置文件时,应该在用户表中为该用户更新电子邮件地址。有人可以在我的代码中发现问题吗?我怀疑这与我发布的最后一行代码有关,但不确定。

谢谢。

4

1 回答 1

0

查看 :accepts_nested_attributes_for 方法,我认为这可能是您正在寻找的

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for

在用户模型中,您可以拥有accepts_nested_attributes_for :user_profile

于 2012-08-19T03:36:53.327 回答