2009-12-04 12 views
11

पॉप्युलेट नहीं किया जा रहा है, मैं RoR सीख रहा हूं और accepts_nested_attributes_for और has_and_belongs_to_many का उपयोग करने की कोशिश कर रहा हूं जो पारंपरिक रूप से दो रूपों की जानकारी सबमिट करने के लिए है। मैंने कुछ साइटों पर पढ़ा है जो वे संगत हैं, कुछ साइटें वे संगत नहीं हैं, और कुछ साइटें नहीं जानती हैं। संदर्भ के रूप में, मैं रेल 2.3.4 का उपयोग कर रहा हूँ। मैंने nested modelsaccepts_nested_attributes_for और has_and_belongs_to_many का उपयोग करने का प्रयास कर रहा है लेकिन जॉइन टेबल को

पर मैंने अपने समाधान को मॉडलिंग करने का प्रयास करने की कोशिश की, जो मैंने डीबग करने का प्रयास किया है, ऐसा लगता है कि मुझे दो समस्याएं हैं लेकिन मुझे यकीन नहीं है कि क्यों।

  1. जब मैं नेस्टेड मॉडल के साथ एक फॉर्म जमा करता हूं, तो नेस्टेड मॉडल जानकारी का केवल एक हिस्सा पोस्ट किया जाता है। मुझे केवल पहला क्षेत्र मिलता है, न कि "n" अन्य उपयोगकर्ता ने
  2. पोस्ट किए गए एकल फ़ील्ड में, एचएबीटीएम रिलेशनशिप के लिए बनाए गए जॉइन टेबल में कोई भी पंक्ति डाली नहीं है।

    अटार्नी मॉडल::

    class Attorney < ActiveRecord::Base 
        has_and_belongs_to_many :associations 
        accepts_nested_attributes_for :associations, :reject_if => proc { |a| a['name'].blank? } 
    end 
    

    एसोसिएशन मॉडल:

    class Association < ActiveRecord::Base 
        has_and_belongs_to_many :attorneys 
        accepts_nested_attributes_for :attorneys 
        validates_presence_of :name, :message => "Please enter an association name." 
    end 
    

    एटोर्नी नियंत्रक:

यहाँ कोड का एक टुकड़ा और मेरी प्रविष्टि प्रयास के लिए इसी लॉग है

def new 
    @attorney = Attorney.new 
    @attorney.associations.build 

    respond_to do |format| 
    format.html # new.html.erb 
    format.xml { render :xml => @attorney } 
    end 
end 

def create 
    @attorney = Attorney.new(params[:attorney]) 

    respond_to do |format| 
    if @attorney.save 
     flash[:notice] = 'Attorney was successfully created.' 
     format.html { redirect_to(@attorney) } 
     format.xml { render :xml => @attorney, :status => :created, :location => @attorney } 
    else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @attorney.errors, :status => :unprocessable_entity } 
    end 
    end 
end 

अटार्नी नया दृश्य:

<% form_for(@attorney, :html => {:multipart => true}) do |f| %> 
    <%= f.error_messages %> 
<%= f.label :"First name" %> 
<%= f.text_field :firstname %><br> 

<%= f.label :"Last Name" %> 
<%= f.text_field :lastname %><br> 

<%= f.label :"Attorney Type" %> 
<%= f.collection_select :member_type_id, MemberType.all, :id, :name %><br> 

<%= f.text_area :bio, :cols => 70, :rows => 20 %><br><br> 

<%= f.label :"Attorney Location" %> 
<%= f.collection_select :office_location_id, OfficeLocation.all, :id, :location %><br> 

<div id="associations"> 
     <%= render :partial => 'shared/membership' %> 
</div> 
<%= add_association_link "Add Association" %> 
    <%= f.submit 'Create' %> 
<% end %> 

सदस्यता आंशिक:

<div class="association"> 
    <% fields_for :associations do |assoc_form| %> 
    <%= assoc_form.collection_select(:association_id, Association.find(:all), :id, :name, :include_blank => true) %> 

<% = link_to_function "निकालें", "$ (this) .up (एसोसिएशन। ') को हटाने()।" %> <% = LINK_TO 'नई एसोसिएशन', new_association_path%> <% अंत%>

अटार्नी हेल प्रति लिंक:

def add_association_link(name) 
    link_to_function name do |page| 
    page.insert_html :bottom, :associations, :partial => 'shared/membership', :object => AssociationsAttorneys.new 
    end 
end 

जुड़ें सारणी उत्प्रवासन:

class CreateAssociationsAttorneys < ActiveRecord::Migration 
    def self.up 
    create_table :associations_attorneys do |t| 
     t.references :attorney, :null => false 
     t.references :association, :null => false 
     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :associations_attorneys 
    end 
end 

लॉग कब्जा:

Processing AttorneysController#new (for 127.0.0.1 at 2009-12-04 08:16:19) [GET] 
Rendering template within layouts/default 
Rendering attorneys/new 
    [4;35;1mMemberType Load (0.4ms)[0m [0mSELECT * FROM "member_types" [0m 
    [4;36;1mOfficeLocation Load (18.6ms)[0m [0;1mSELECT * FROM "office_locations" [0m 
    [4;35;1mAssociation Load (0.6ms)[0m [0mSELECT * FROM "associations" [0m 
Rendered shared/_membership (3.5ms) 
    [4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM "associations" [0m 
Rendered shared/_membership (1.5ms) 
Rendered shared/_nav (0.6ms) 
Rendered shared/_footer (0.1ms) 
Completed in 149ms (View: 114, DB: 20) | 200 OK [http://localhost/attorneys/new] 

Processing ApplicationController#index (for 127.0.0.1 at 2009-12-04 08:16:19) [GET] 

Processing AttorneysController#create (for 127.0.0.1 at 2009-12-04 08:16:57) [POST] 
    Parameters: {"commit"=>"Create", "authenticity_token"=>"Jh7aMCcOY7jUu/D1YtiCswg2n6iwqnS98VnVn46psp0=", "associations"=>{"association_id"=>"3"}, "attorney"=>{"birthstate"=>"Alabama", "office_location_id"=>"1", "birthdate"=>"December 3, 2009", "birthcity"=>"Test", "middlename"=>"Test", "lastname"=>"Testing", "image_temp"=>"", "member_type_id"=>"2", "firstname"=>"Test", "bio"=>"testing testing testing", "suffix"=>"", "email"=>"[email protected]"}} 
    [4;35;1mAttorney Load (15.6ms)[0m [0mSELECT "attorneys".id FROM "attorneys" WHERE ("attorneys"."email" = '[email protected]') LIMIT 1[0m 
    [4;36;1mAttorney Create (0.8ms)[0m [0;1mINSERT INTO "attorneys" ("birthstate", "created_at", "birthdate", "office_location_id", "birthcity", "updated_at", "middlename", "lastname", "firstname", "member_type_id", "suffix", "bio", "image", "email") VALUES('Alabama', '2009-12-04 15:16:57', 'December 3, 2009', 1, 'Test', '2009-12-04 15:16:57', 'Test', 'Testing', 'Test', 2, '', 'testing testing testing', NULL, '[email protected]')[0m 
Redirected to http://localhost:3000/attorneys/11 
Completed in 150ms (DB: 16) | 302 Found [http://localhost/attorneys] 

मैं देख सकता हूँ कि संघों "=> {" association_id "=>" 3 "} यह केवल विशेष व्यक्ति के लिए मेरे पास कई संगठनों का अंतिम भाग प्राप्त हो रहा है और यह शामिल तालिका में कोई प्रविष्टि नहीं बना रहा है। मेरा कोड कहां गलत हो सकता है?

उत्तर

27

आप दोनों को यहां समस्याएं हैं, दुर्भाग्यवश उनमें से एक दूसरे द्वारा मुखौटा है।

दोनों समस्याओं को देखने के इस भाग से स्टेम:

<div class="association"> 
    <% fields_for :associations do |assoc_form| %> 
    <%= assoc_form.collection_select(:association_id, Association.find(:all), 
     :id, :name, :include_blank => true) %> 

समस्या 1: आप गलत समझा गया है accept_nested_fields_for क्या करता है।

accepts_nested_fields_for किसी प्रपत्र में संबंधित वस्तुओं को बनाने और संशोधित करने के लिए उपयोग किया जाता है। इसका उपयोग तालिका में शामिल होने के लिए किया जा सकता है, जो कि आप जो करने की कोशिश कर रहे हैं। हालांकि, शामिल तालिका को पॉप्युलेट करने के लिए accepts_nested_fields_for का उपयोग करना एक HABTM रिश्ते के साथ असंभव है। Accepts_nested_fields_for का एक अच्छा उपयोग होगा यदि आप एक नया एसोसिएशन बनाना चाहते हैं जो नए अटॉर्नी से जुड़ा होगा। या यदि आपके पास एक समृद्ध मॉडल शामिल था जिसके लिए प्रत्येक रिकॉर्ड के लिए अतिरिक्त जानकारी की आवश्यकता होती है।

समस्या 2: आप इस फॉर्म में फ़ील्ड को अटॉर्नी फॉर्म से लिंक नहीं कर रहे हैं। Accepts_nested_fields_for का उपयोग करने के लिए आवश्यक है।

हम पहले से ही स्थापित कर चुके हैं कि accepts_nested_fields_for यह नहीं है कि आपको इसे पूरा करने की आवश्यकता है, लेकिन, आप अभी भी फॉर्म के साथ select Association_id फ़ील्ड को संबद्ध नहीं कर रहे हैं। यही कारण है कि पैराम्स [एसोसिएशन] [एसोसिएशन] सेट किया गया था और पैरा [वकील] [एसोसिएशन] [एसोसिएशन] नहीं था।

समस्या 3: फ़ॉर्म संरचना यह सब कुछ गलत है कि ऐसा लगता है कि आप इसे पूरा करने की कोशिश कर रहे हैं।

एक बहुत अधिक है जो उचित ब्रेक डाउन देने के लिए मेरे लिए सही करने की आवश्यकता है। आप complex-forms-example repository को जांचने से बेहतर हैं। यह accepts_nested_attributes_for का एक कामकाजी उदाहरण है, यह किसी भी एचएबीटीएम संबंधों से निपटता नहीं है, लेकिन यह आपको हर चीज़ को सिखाना चाहिए जो आपको जानने की जरूरत है। आपको आवश्यकतानुसार सही कोड 90% है। उपरोक्त लिंक किए गए जटिल-रूप-उदाहरण आपको सिखाएंगे कि आपको add_association_link और create_association_link वाले रिक्त स्थान भरने के लिए क्या पता होना चाहिए।

सुधार निम्नलिखित चरण शामिल हैं:

  1. एक मॉडल में शामिल होने बनाएँ, और एक के माध्यम से रिश्ते बदल एक कई है करने के लिए, में शामिल होने के मॉडल पर नेस्टेड विशेषताओं को स्वीकार।
  2. भवनों के निर्माण के मामले में नियंत्रक में मामूली समायोजन करें।
  3. आंशिक रूप से फॉर्म बिल्डर ऑब्जेक्ट पास करें।
  4. आंशिक रूप में फॉर्म को फिर से लिखें ताकि यह नव निर्मित शामिल मॉडल पर केंद्रित हो।

आप इसे निम्नलिखित परिवर्तनों के साथ पूरा कर सकते हैं।

class Attorney < ActiveRecord::Base 
    has_many :attorney_associations 
    has_many :associations, :through => :attorney_associations 

    accepts_nested_attributes_for :attorney_associations, :reject_if => proc { |a| 
    a['association_id'].blank? } 
    accepts_nested_attributes_for :associations, :reject_if => proc {|a| 
    a['name'].blank?} 
end 

class AttorneyAssociations < ActiveRecord::Base 
    belongs_to :attorney 
    belongs_to :association 
end 

अटार्नी नियंत्रक:

def new 
    @attorney = Attorney.new 
    @attorney.associations.build 
    @attorney.attorney_associations.build 


    respond_to do |format| 
    format.html # new.html.erb 
    format.xml { render :xml => @attorney } 
    end 
end 

नए अटार्नी दृश्य:

<% form_for(@attorney, :html => {:multipart => true}) do |f| %> 
    <%= f.error_messages %> 
<%= f.label :"First name" %> 
<%= f.text_field :firstname %><br> 

<%= f.label :"Last Name" %> 
<%= f.text_field :lastname %><br> 

<%= f.label :"Attorney Type" %> 
<%= f.collection_select :member_type_id, MemberType.all, :id, :name %><br> 

<%= f.text_area :bio, :cols => 70, :rows => 20 %><br><br> 

<%= f.label :"Attorney Location" %> 
<%= f.collection_select :office_location_id, OfficeLocation.all, :id, :location %><br> 

<div id="associations"> 
    <% f.fields_for :attorney_association do |aa_form| %> 
    <%= render :partial => 'attorney_association', :locals => {:f => aa_form} %> 
    <% end %> 
    <%= add_association_link "Add Another Existing Association" %> 
    <% f.fields_for :associations do |assoc_form| %> 
    <%= render :partial => 'attorney', :locals => {:f => assoc_form} %>  
    <%= create_association_link, "Create a New Association for this Attorney" %> 
</div> 



<%= f.submit 'Create' %> 
<% end %> 

मुझे लगता है कि add_association_link संभालने कर रहा हूँ एक जावास्क्रिप्ट सहायक है कि एक खाली क्लोन करने के लिए लिंक प्रदान करता है है सदस्यता आंशिक क्या था इसका उदाहरण। create_association_link एक समान सहायक के लिए एक प्लेसहोल्डर है जो एक नए संगठन के लिए आंशिक जोड़ देगा।

अटार्नी एसोसिएशन आंशिक:

<div class="attorney_association"> 
    <%= f.collection_select(:association_id, Association.find(:all), 
     :id, :name, :include_blank => true) %> 
    <%= link_to_function "remove", "$(this).up('.attorney_association').remove()" %> 
    </div> 

एसोसिएशन आंशिक:

<div class="association"> 
    <%= f.label_for :name %> 
    <%= f.text_field :name %> 
    <%= link_to_function "remove", "$(this).up('.attorney_association').remove()" %> 
    </div> 
+0

प्रपत्र 'एफ' में पारित करने के लिए दृश्य बदलने से, मैं अब हो रही है त्रुटि: अपरिभाषित # <एसोसिएशन: 0x1031aff28> के लिए आंशिक रूप से इस पंक्ति पर विधि 'association_id': <% = assoc_form.collection_select (: association_id, Association.find (: all), id ,: नाम,: include_blank => सत्य)%>। कोई विचार? – davidstites

+0

ऐसा लगता है कि मैंने कई अन्य समस्याओं को अनदेखा कर दिया था जिन्हें आप चल रहे थे। मैंने उन लोगों को संबोधित करने के लिए समाधान भी अपडेट किया है। – EmFi

+0

इसलिए मैंने इस पर कई हफ्तों तक काम किया है और मुझे अभी भी समस्याएं हैं। जब यह अटॉर्नी_सासिएशन आंशिक रूप से प्रस्तुत करने का प्रयास करता है, तो यह जानने के बारे में शिकायत करता है कि 'एफ' क्या है ... मैंने सोचा कि फॉर्म ऑब्जेक्ट को पास किया गया है: स्थानीय हैश? – davidstites

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

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