我无法获取嵌套表单中给出的属性。我的确切配置无法找到与此问题相关的任何其他帖子:
以下是缩写模型。如您所见,用户和组织之间的关联有点复杂:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
attr_accessible :email, :password, :password_confirmation, :remember_me
attr_accessible :first_name, :last_name, :nickname
attr_accessible :current_organization_attributes
has_many :created_organizations, :class_name => "Organization", :foreign_key => :creator_user_id, :inverse_of => :creator_user
belongs_to :current_organization, :class_name => "Organization", :foreign_key => :current_org_id # one-way relationship
accepts_nested_attributes_for :current_organization
...
end
class Organization < ActiveRecord::Base
belongs_to :creator_user, :class_name => "User", :foreign_key => "creator_user_id", :inverse_of => :created_organizations
...
end
缩写模式:
# == Schema Information
#
# Table name: users
#
# id :integer(4) not null, primary key
# email :string(255) default(""), not null
# current_org_id :integer(4)
# first_name :string(255)
# last_name :string(255)
# nickname :string(255)
#
# == Schema Information
#
# Table name: organizations
#
# id :integer(4) not null, primary key
# name :string(100)
# creator_user_id :integer(4) not null
# zip_code :string(255)
# phone_number :string(255)
#
这是缩写视图代码(HAML):
#main
%h1 Your account
= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :autocomplete => "off" }) do |f|
= devise_error_messages!
.edit-account-hold
%span About you
= f.label :first_name, "First Name"
= f.text_field :first_name, :required => @contact_request_is_pending
...
.edit-account-hold
%span Your business
= f.fields_for :current_organization do |o|
= o.label :zip_code, "Zip Code"
= o.text_field :zip_code, :required => @contact_request_is_pending
= o.label :phone_number, "Phone Number"
= o.text_field :phone_number, :required => @contact_request_is_pending
.edit-account-hold
%span Your password
= f.label :password
= f.password_field :password
= f.label :password_confirmation
= f.password_field :password_confirmation
= f.label :current_password
= f.password_field :current_password
= f.submit "Update", :class => "orange-button border-radius"
控制器代码:
def update
if resource.update_with_or_without_password_as_needed(params[resource_name])
set_flash_message :notice, :updated
redirect_to after_update_path_for(resource)
else
clean_up_passwords(resource)
render_with_scope :edit
end
end
以下是单击“更新”时传递的参数样本:
{"utf8"=>"✓", "authenticity_token"=>"8aL7bMJdVI2uaLt3WoZEraSB0U5iZgBvxYh5fwsQnqM=", "user"=>{"first_name"=>"Nick", "last_name"=>"", "nickname"=>"", "email"=>"nick@mycompany.com", "current_organization_attributes"=>{"zip_code"=>"12345", "phone_number"=>"1112223333", "id"=>"1000003"}, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]"}, "commit"=>"Update"}
嵌套hash params [“user”] [“current_organization_attributes”]包含无法保存的属性。在服务器中使用调试器,我已确认调用
user.current_organization.update_attributes( the_above_mentioned_current_organization_attributes_hash )
在适当的时候工作得很好并且像我想的那样更新属性。系统为什么不自动执行此操作?是否与使用备用类名或外键的belongs_to关系有关?
帮助!
答案 0 :(得分:0)
IIRC,accepts_nested_attributes_for
仅适用于has_one
或has_many
个关联...至少,这是对文档的粗略一瞥。我不认为它适用于belongs_to
。
我最好的建议是更改用户模型中的关联(这也需要更改架构)来执行以下操作:
has_many :organizations
has_one :current_organization, :class_name => "Organization", :conditions => {:current => true}
organizations
表中有一个名为current
的列,并验证只有一个组织可以是最新的,可能还有一个回调可以更改该用户组织的所有其他current
值如果列已更新,则显示false
(显然限定为该用户的范围)。
这应该允许fields_for
和accepts_nested_attributes_for
按预期工作。