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
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
When I try to load / account / new, I get the following exception:
NameError in Accounts
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
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.
ruby ruby-on-rails nested-attributes associations
Billy Gray Aug 21 '09 at 17:08 2009-08-21 17:08
source share