पॉप्युलेट नहीं किया जा रहा है, मैं RoR सीख रहा हूं और accepts_nested_attributes_for और has_and_belongs_to_many का उपयोग करने की कोशिश कर रहा हूं जो पारंपरिक रूप से दो रूपों की जानकारी सबमिट करने के लिए है। मैंने कुछ साइटों पर पढ़ा है जो वे संगत हैं, कुछ साइटें वे संगत नहीं हैं, और कुछ साइटें नहीं जानती हैं। संदर्भ के रूप में, मैं रेल 2.3.4 का उपयोग कर रहा हूँ। मैंने nested modelsaccepts_nested_attributes_for और has_and_belongs_to_many का उपयोग करने का प्रयास कर रहा है लेकिन जॉइन टेबल को
पर मैंने अपने समाधान को मॉडलिंग करने का प्रयास करने की कोशिश की, जो मैंने डीबग करने का प्रयास किया है, ऐसा लगता है कि मुझे दो समस्याएं हैं लेकिन मुझे यकीन नहीं है कि क्यों।
- जब मैं नेस्टेड मॉडल के साथ एक फॉर्म जमा करता हूं, तो नेस्टेड मॉडल जानकारी का केवल एक हिस्सा पोस्ट किया जाता है। मुझे केवल पहला क्षेत्र मिलता है, न कि "n" अन्य उपयोगकर्ता ने
- पोस्ट किए गए एकल फ़ील्ड में, एचएबीटीएम रिलेशनशिप के लिए बनाए गए जॉइन टेबल में कोई भी पंक्ति डाली नहीं है।
अटार्नी मॉडल::
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 "} यह केवल विशेष व्यक्ति के लिए मेरे पास कई संगठनों का अंतिम भाग प्राप्त हो रहा है और यह शामिल तालिका में कोई प्रविष्टि नहीं बना रहा है। मेरा कोड कहां गलत हो सकता है?
प्रपत्र 'एफ' में पारित करने के लिए दृश्य बदलने से, मैं अब हो रही है त्रुटि: अपरिभाषित # <एसोसिएशन: 0x1031aff28> के लिए आंशिक रूप से इस पंक्ति पर विधि 'association_id': <% = assoc_form.collection_select (: association_id, Association.find (: all), id ,: नाम,: include_blank => सत्य)%>। कोई विचार? – davidstites
ऐसा लगता है कि मैंने कई अन्य समस्याओं को अनदेखा कर दिया था जिन्हें आप चल रहे थे। मैंने उन लोगों को संबोधित करने के लिए समाधान भी अपडेट किया है। – EmFi
इसलिए मैंने इस पर कई हफ्तों तक काम किया है और मुझे अभी भी समस्याएं हैं। जब यह अटॉर्नी_सासिएशन आंशिक रूप से प्रस्तुत करने का प्रयास करता है, तो यह जानने के बारे में शिकायत करता है कि 'एफ' क्या है ... मैंने सोचा कि फॉर्म ऑब्जेक्ट को पास किया गया है: स्थानीय हैश? – davidstites