我看到很多关于使用嵌套表单保存的东西,但它们都与我的关系相反。客户信息属于_to 用户,但我想从客户信息更新用户电子邮件。现在视图正在工作,只是没有保存。
我定义了以下关系:
class User < ActiveRecord::Base
has_one :customer_info, dependent: :destroy
accepts_nested_attributes_for :customer_info
end
class CustomerInfo < ActiveRecord::Base
belongs_to :user
attr_accessible :user, :email
accepts_nested_attributes_for :user
end
以及以下嵌套形式:
%h1 Editing customer_info
= form_for @customer_info, :validate => true do |f|
- if @customer_info.errors.any?
#error_explanation
%h2= "#{pluralize(@customer_info.errors.count, "error")} prohibited this user from being saved:"
%ul
- @customer_info.errors.full_messages.each do |msg|
%li= msg
%h2 Your Profile
= fields_for @user do |i|
.field
= i.label :email, 'Email'
= i.text_field :email
.field
= f.label :username, "Username"
= f.text_field :username
.actions
= f.submit 'Next'