2011-08-10 161 views
8

我仍然有嵌套窗體的問題。這裏是我的表單代碼:雙嵌套模型表格

<%= 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 %> 

賬戶模式:

class Account < ActiveRecord::Base 

has_many :organizations 

accepts_nested_attributes_for :organizations 
end 

的組織模式:

class Organization < ActiveRecord::Base 

belongs_to :account 

has_many :locations 

accepts_nested_attributes_for :locations 
end 

和定位模式:

class Location < ActiveRecord::Base 

belongs_to :organization 

end 

最後,帳戶控制器:

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 

這裏是我得到的錯誤:

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"} 

任何幫助,這將不勝感激。我現在一直在看這幾個小時。

回答

13

你應該在嵌套形式使用新的構建嵌套嵌套形式:)):

<%= 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 %> 
+1

這工作!非常感謝您的幫助。 –

1

的甚至機解決方案:

1)在你的Gemfile,列表nested_form作爲一個依賴。

2)在你的模型做:

class Account < ActiveRecord::Base 
    accepts_nested_attributes_for :organizations 
end 

class Organization < ActiveRecord::Base 
    accepts_nested_attributes_for :locations 
end 

3)創建組織定期的形式./app/view/organizations/ and for Location under下您的賬戶形式/應用/視圖/位置/`

4),這樣做:(!它變得很短這樣)

<%= 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)在你的組織形式,這樣做:(也很短)

<%= 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 %> 

檢查這些RailsCasts:

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

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