I cannot get the attributes given in a nested form to take. I haven't been able to find any other posts related to this issue with my exact configuration:
Here are the abbreviated models. As you can see, the associations between Users and Organizations are a bit complex:
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
And abbreviated schemas:
# == 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)
#
Here is the abbreviated view code (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.lab开发者_StackOverflowel :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"
Controller code:
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
Here is a sample of the params that are passed when "Update" is clicked:
{"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"}
The nested hash params["user"]["current_organization_attributes"] contains the attributes that are failing to save. Using the debugger within the server, I have confirmed that a call to
user.current_organization.update_attributes( the_above_mentioned_current_organization_attributes_hash )
at the appropriate time works great and updates the attributes just like I'd like. Why won't the system do this automatically? Is it something to do with the belongs_to relationship using alternate class names or foreign keys?
Help!
IIRC, accepts_nested_attributes_for
only works for has_one
or has_many
associations... at least, that's what a cursory glance at the docs reveals. I don't think it works on belongs_to
.
My best suggestion is to change the association in the User model (and this requires schema changes as well) to do something like:
has_many :organizations
has_one :current_organization, :class_name => "Organization", :conditions => {:current => true}
Where the organizations
table has a column called current
, and validate that only one organization can be current, maybe with a callback that changes all other current
values for that users' organizations to false
if the column is updated (scoped to that user, obviously).
This should allow fields_for
and accepts_nested_attributes_for
to work as intended.
精彩评论