2011-08-10 19 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 

और अंत में, लेखा नियंत्रक:

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

इस के साथ किसी भी मदद की बहुत सराहना की जाएगी:

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 

यहाँ त्रुटि मैं हो रही है। मैं इसे कुछ घंटों के लिए देख रहा हूं।

उत्तर

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 एक निर्भरता के रूप में। ।

class Account < ActiveRecord::Base 
    accepts_nested_attributes_for :organizations 
end 

class Organization < ActiveRecord::Base 
    accepts_nested_attributes_for :locations 
end 

3)/ऐप्स/देखें/अपने खाता रूप में स्थानों/`

4) ./app/view/organizations/ and for Location under के तहत संगठन के लिए नियमित रूप से रूपों बनाने के लिए, ऐसा करते हैं:

2) अपने मॉडल में ऐसा : भी बहुत छोटा()

012,351:) अपने संगठन के रूप में (! यह बहुत छोटा इस तरह से हो जाता है)

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

संबंधित मुद्दे