Getting fields_for and accepts_nested_attributes_for to work with the own_to relation

I can't seem to get a nested form to create in the rail view for belongs_to relationship using the new accepts_nested_attributes_for for Rails 2.3. I looked at many of the resources available, and it looks like my code should work, but fields_for explodes on me, and I suspect it has something to do with how I set up the nested models.

The error I hit is a common one, which can have many reasons:

 '@account[owner]' is not allowed as an instance variable name 

Here are two models:

 class Account < ActiveRecord::Base # Relationships belongs_to :owner, :class_name => 'User', :foreign_key => 'owner_id' accepts_nested_attributes_for :owner has_many :users end class User < ActiveRecord::Base belongs_to :account end 

Perhaps this is where I am doing this “rong”, because an account can have “owners” and “users”, but the user has only one “account” based on the user model account_id.

This is the view code in new.html.haml that blows me up:

 - form_for :account, :url => account_path do |account| = account.text_field :name - account.fields_for :owner do |owner| = owner.text_field :name 

And this is the controller code for the new action:

 class AccountsController < ApplicationController # GET /account/new def new @account = Account.new end end 

When I try to load / account / new, I get the following exception:

 NameError in Accounts#new Showing app/views/accounts/new.html.haml where line #63 raised: @account[owner] is not allowed as an instance variable name 

If I try to use the mysterious 'build' method, it just runs in the controller, possibly because the assembly is only for relationships with multiple records:

 class AccountsController < ApplicationController # GET /account/new def new @account = Account.new @account.owner.build end end You have a nil object when you didn't expect it! The error occurred while evaluating nil.build 

If I try to set this using @ account.owner_attributes = {} in the controller or @ account.owner = User.new, I will return to the original error, "@account [owner] is not resolved as an instance variable name".

Does anyone else have a new accepts_nested_attributes_for method working with the own_to relation? Is there anything special or other that you need to do? All official and code examples (like great stuff in Ryans Scraps ) are associated with multi-record associations.

+26
ruby ruby-on-rails nested-attributes associations
Aug 21 '09 at 17:08
source share
4 answers

I think your accepts_nested_attributes is on the wrong side of the relationship. Maybe something like this will work?

 class Account < ActiveRecord::Base belongs_to :owner, :class_name => 'User', :foreign_key => 'owner_id' has_many :users end class User < ActiveRecord::Base belongs_to :account has_one :account, :foreign_key => :owner_id accepts_nested_attributes_for :account end 

To create an account you want to use build_account.

You can see more examples in the docs .

+8
Aug 21 '09 at 17:33
source share

I was a little late for a few months, but I was looking for this error, and my situation was that I could not change my attitude in order to "look the other way."

The answer is actually quite simple, you have to do it in your new action:

 @account.build_owner 

The reason the form was not displayed using the search_fields was because it did not have a valid object. There was a correct idea:

 @account.owner.build 

However, this does not work belongs_to . This method is generated only with has_many and has_and_belongs_to_many .

Link: http://guides.rubyonrails.org/association_basics.html#belongs-to-association-reference

+42
Mar 14 '10 at 7:16
source share

I am using Rails 2.3.5 and I noticed the same thing. Checking the source for active_record nested_attributes.rb, it looks like role_to should work fine. So it looks like it could be a "nested forms" error.

I have a nested form similar to yours, with User belongs_to :address and Address independent of the user.

Then in the form I just <% f.fields_for :address_attributes do |address_form| %> <% f.fields_for :address_attributes do |address_form| %> instead of <% f.fields_for :address do |address_form| %> <% f.fields_for :address do |address_form| %> . Temporary hacking until there is a better way, but it works. The accepts_nested_attributes_for method expects params to include something like:

{user=>{address_attributes=>{attr1=>'one',attr2=>'two'}, name=>'myname'}

... but fields_for produces:

{user=>{address=>{attr1=>'one',attr2=>'two'}, name=>'myname'}

Thus, you do not need to add this has_one :account to your code, which does not work in my case.

Update: found a better answer:

Here is the gist of the code that I use to work correctly:

Rails Nested forms with own_to Gist attribute

Hope this helps.

+8
Jan 13 '10 at 0:12
source share
 class Account < ActiveRecord::Base belongs_to :owner :class_name => 'User', :foreign_key => 'owner_id' end 

This works for me .: foreign_key => 'owner_id' was the key issue in my case.

-3
Aug 19 '10 at 14:52
source share



All Articles