9

I am still having problems with nested forms. Here is my form code:

<%= form_for @account do |f| %>

<%= f.label :account_type %><br />
<%= f.text_field :account_type %><br />

    <%= f.fields_for :organizations do |builder| %>
        <%= builder.label :name %><br />
        <%= builder.text_field :name %><br />
        <%= builder.label :website %><br />
        <%= builder.text_field :website %><br />

        <%= f.fields_for :locations do |builder| %>
            <%= builder.label :phone %><br />
            <%= builder.text_field :phone %><br />
            <%= builder.label :toll_free_phone %><br />
            <%= builder.text_field :toll_free_phone %><br />
            <%= builder.label :fax %><br />
            <%= builder.text_field :fax %><br />
        <% end %>
    <% end %>

<%= f.submit "Add account" %>
<% end %>

The Account model:

class Account < ActiveRecord::Base

has_many :organizations

accepts_nested_attributes_for :organizations
end

The Organization model:

class Organization < ActiveRecord::Base

belongs_to :account

has_many :locations

accepts_nested_attributes_for :locations
end

And the Location model:

class Location < ActiveRecord::Base

belongs_to :organization

end

And lastly, the Accounts Controller:

class AccountsController < ApplicationController

def new
    @account = Account.new
    organization = @account.organizations.build
    organization.locations.build

    @header = "Create account"
end

def create
    @account = Account.new(params[:account])
    if @account.save
        #handle success
    else
        render 'new'
    end
end
end

Here is the error I am getting:

ActiveRecord::UnknownAttributeError in AccountsController#create

unknown attribute: locations
Rails.root: C:/Documents and Settings/Corey Quillen/My       
Documents/rails_projects/shop_manager

Application Trace | Framework Trace | Full Trace
app/controllers/accounts_controller.rb:12:in `new'
app/controllers/accounts_controller.rb:12:in `create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"xuZLSP+PSjdra3v51nIkJYZeXRM0X88iF135hPlp4sc=",
 "account"=>{"account_type"=>"Company",
 "organizations_attributes"=>{"0"=>{"name"=>"Atlas",
 "website"=>"www.atlas.com"}},
 "locations"=>{"phone"=>"555-555-5555",
 "toll_free_phone"=>"800-555-5555",
 "fax"=>"512-555-5555"}},
 "commit"=>"Add account"}

Any help with this will be greatly appreciated. I have been looking at this now for a couple of hours.

Coder
  • 1,917
  • 3
  • 17
  • 33
Corey Quillen
  • 1,566
  • 4
  • 24
  • 52

2 Answers2

17

You should use new builder in nested form for nested nested form :)) :

<%= form_for @account do |f| %>

<%= f.label :account_type %><br />
<%= f.text_field :account_type %><br />

    <%= f.fields_for :organizations do |builder| %>
        <%= builder.label :name %><br />
        <%= builder.text_field :name %><br />
        <%= builder.label :website %><br />
        <%= builder.text_field :website %><br />

        <%= builder.fields_for :locations do |lb| %>
            <%= lb.label :phone %><br />
            <%= lb.text_field :phone %><br />
            <%= lb.label :toll_free_phone %><br />
            <%= lb.text_field :toll_free_phone %><br />
            <%= lb.label :fax %><br />
            <%= lb.text_field :fax %><br />
        <% end %>
    <% end %>

<%= f.submit "Add account" %>
<% end %>
antonversal
  • 1,219
  • 9
  • 10
1

An even DRYer solution:

1) in your Gemfile, list nested_form as a dependency.

2) in your models do this:

class Account <  ActiveRecord::Base
  accepts_nested_attributes_for :organizations
end

class Organization < ActiveRecord::Base
  accepts_nested_attributes_for :locations
end

3) create regular forms for Organization under ./app/view/organizations/ and for Location under./app/view/locations/`

4) in your Account form, do this: (it gets pretty short this way!)

<%= nested_form_for @account do |f| %>

<%= f.label :account_type %><br />
<%= f.text_field :account_type %><br />

 <%= f.fields_for :organizations %>

<%= f.submit "Add account" %>
<% end %>

5) in your Organization form, do this: (also pretty short)

<%= nested_form_for @organization do |f| %>
<%= f.label :name %><br />
    <%= f.text_field :name %><br />
    <%= f.label :website %><br />
    <%= f.text_field :website %><br />

    <%= f.fields_for :locations %>

<%= f.submit "Add Organization" %>
<% end %>

Check these RailsCasts:

http://railscasts.com/episodes/196-nested-model-form-part-1

http://railscasts.com/episodes/197-nested-model-form-part-2

Tilo
  • 33,354
  • 5
  • 79
  • 106