6

Update: answered here fields_for।रेल 3: nested_form, collection_select, accepts_nested_attributes_for और

यहां बहुत अच्छे प्रश्न और उत्तर हैं और nested_form, collection_select, accepts_nested_attributes_for और field_for अच्छी तरह से खेलने के लिए interweb पर हैं, लेकिन मैं अभी भी स्टंप हूं। अगर आप मेरी मदद कर सकते हैं तो अग्रिम धन्यवाद।

उद्देश्य: एक नया आईएसबीएन रिकॉर्ड उत्पन्न करने के लिए। एक आईएसबीएन में कई योगदानकर्ता हो सकते हैं। मैं आवश्यक रूप से एक फॉर्म पर नए योगदानकर्ता फ़ील्ड को गतिशील रूप से उत्पादित करने के लिए ryanb nested_form gem का सफलतापूर्वक उपयोग कर रहा हूं। इन क्षेत्रों में से एक योगदानकर्ता में सभी नाम रिकॉर्ड्स के संग्रह_ चयन का उपयोग करता है। जब नया रिकॉर्ड बनाया जाता है, तो कई योगदानकर्ता आईडी को शामिल तालिका (योगदानकर्ता_आईएसबीएनएस) में लिखा जाना चाहिए।

मैं इस काम के बिट्स मिल गया है, लेकिन केवल बिंदु है जहां मैं ISBNs तालिका में नया रिकार्ड के लिए एक एकल योगदानकर्ता आईडी बचा सकता है। मुझे शामिल तालिका में किसी भी डेटा को लिखने में कहीं भी नहीं मिल रहा है।

मेरे पास तीन मॉडल हैं। योगदानकर्ताओं और इस्बन्स के पास कई रिश्ते हैं, जो मैंने has_many का उपयोग करके किया है: के माध्यम से। एक आईएसबीएन में कई योगदानकर्ता हो सकते हैं, और एक योगदानकर्ता के पास कई आईएसबीएन हो सकते हैं। वे योगदानकर्ताओं के माध्यम से शामिल हो गए हैं।

isbn.rb

attr_accessible    :contributor_id 
    has_many      :contributors, :through => :contributors_isbns 
    has_many      :contributors_isbns 
    accepts_nested_attributes_for :contributors 
    accepts_nested_attributes_for :contributors_isbns 

contributor.rb

attr_accessible    :isbn_id 
    has_many      :contributors_isbns 
    has_many      :isbns, :through => :contributors_isbns 
    accepts_nested_attributes_for :isbns 

contributors_isbn.rb

class ContributorsIsbn 
    attr_accessible    :isbn_id, :contributor_id 
    belongs_to     :isbn 
    belongs_to     :contributor 
    accepts_nested_attributes_for :contributors 

ISBN के नियंत्रक में:

def new 
    @isbn = Isbn.new 
    @title = "Create new ISBN" 
    1.times {@isbn.contributors.build} 
    @isbn.contributors_isbns.build.build_contributor 
    end 

(जाहिर है मैं अपने मन जिस पर उपयोग करने के लिए विधि निर्माण नहीं कर सकते।)

ISBNs new.html.erb ध्यान में रखते हुए:

<%= nested_form_for @isbn, :validate => false do |f| %> 
<h1>Create new ISBN</h1> 
<%= render 'shared/error_messages', :object => f.object %> 
<%= render 'fields', :f => f %> 
    <div class="actions"> 
    <%= f.submit "Create" %> 
    </div> 

<% end %> 

_fields आंशिक रूप से, एक के साथ एक संस्करण बहुत सादा text_field:

<%= field_set_tag 'Contributor' do %> 
<%= f.link_to_add "Add Additional Contributor", :contributors %> 
<li> 
<%= f.label 'Contributor Sequence Number' %> 
<%= f.text_field :descriptivedetail_contributor_sequencenumber%> 
</li> 

<%= f.fields_for :contributors_isbns, :validate => false do |contrib| %> 
<li> 
<%= contrib.label :id, 'contributors_isbns id' %> 
<%= contrib.text_field :id %> 
</li> 
<% end %> 

<li> 
<%= f.label 'Contributor Role' %> 
<%= f.text_field :descriptivedetail_contributor_contributorrole %> 
</li> 

<% end %> 

और यहाँ, एक सजावटी संस्करण है जो या तो काम नहीं करता:

<%= f.fields_for :contributors_isbns, :validate => false do |contributors| %> 
<li> 
<%= f.label :personnameinverted, 'Contributor Name' %> 
<%= f.collection_select(:contributor_id, Contributor.all, :id, :personnameinverted) %> 
</li> 
<% end %> 

यह कोड here से उत्तर का उपयोग करता है। nested_form_for @isbn लाइन पर एक 'खोई ब्लॉक "त्रुटि में दोनों परिणाम

धन्यवाद इतना फिर से अग्रिम में

अद्यतन:।। here nested_form मणि जो इस तरह की पर देख रहे हैं के लिए काम में आ सकते हैं के बारे में कुछ जानकारी है समस्या। और यहां एक [200 9 लेकिन अभी भी प्रासंगिक पोस्ट] है [4] accepts_nested_attributes_for पर।

अपडेट 2: ठीक है, यहाँ एक बात है। मैं दो अलग-अलग मॉडलों में इसका एक कट-डाउन संस्करण पर चारों ओर घूम रहा हूं, collection_select या has_many के माध्यम से उपयोग नहीं कर रहा है, लेकिन केवल एक साधारण belong_to/has_many एसोसिएशन पर है। मूल मॉडल अनुबंध है और बाल मॉडल Istc है। मैं एक आरईसी भी नहीं बना सका नेस्टेड रूप के माध्यम से ord। हालांकि, स्टैक में देखने और त्रुटि संदेश को गुगल करने के बाद "चेतावनी।सुरक्षित गुणों को बड़े पैमाने पर असाइन नहीं किया जा सकता है "मैंने अभी :istcs_attributes को :attr_accessible लाइन में जोड़ा है और अब मैं रिकॉर्ड जोड़ सकता हूं। बल्कि एक महत्वपूर्ण बिट लापता है, और आरटीएफएम का मामला है, क्योंकि यह मणि रीडमे में ठीक है। अगर इस संघ के माध्यम से और अधिक जटिल has_many पर काम करता है देखने के लिए बाद अद्यतन

अद्यतन 4:। [यहाँ] [5] कैसे एक शून्य रिकॉर्ड त्रुटि संदेश के साथ सौदा करने के बारे में एक और उपयोगी पोस्ट है

अद्यतन 5।: थोड़ी सी चक्कर - जब मैंने गतिशील रूप से फार्म के लिए एक नया सेट जोड़ा, तो बच्चे के एक रिकॉर्ड में से एक बनाया जा रहा था। दुह - मेरे पास बच्चे के फॉर्म क्षेत्र के अंदर "जोड़ें" लिंक था। यहां पहले है:

<%= f.fields_for :istcs do |istc_form| %> 
<h4> Istc</h4> 
<%= istc_form.label "istc name" %> 
<%= istc_form.text_field :title_title_text %> 
<%= istc_form.link_to_remove "[-] Remove this istc"%> 
<%= f.link_to_add "[+] Add an istc", :istcs %> 
<% end %> 

<%= f.fields_for :istcs do |istc_form| %> 
<h4> Istc</h4> 
<%= istc_form.label "istc name" %> 
<%= istc_form.text_field :title_title_text %> 
<%= istc_form.link_to_remove "[-] Remove this istc"%> 
<% end %> 
<%= f.link_to_add "[+] Add an istc", :istcs %> 

अद्यतन 6, बाद जवाब:

ओह नहीं

और यहाँ के बाद है। Collection_select काम नहीं कर रहा है। यह योगदानकर्ता मॉडल से मौजूदा एक का उपयोग नहीं कर रहा है, नए योगदानकर्ता रिकॉर्ड जोड़ रहा है। Someone else भी यह समस्या थी। कोई विचार?

उत्तर

3

हूजाह! यहां कोड है जो यह सब काम करता है। बिट वर्बोज़ लेकिन कुछ भी छोड़ना नहीं चाहता था। मेरा मुख्य सीख:

  • आप बच्चे को माता-पिता मॉडल में attr_accessible जिम्मेदार बताते हैं बनाने की जरूरत है

  • आप माता-पिता और बच्चे आईडी बनाने की जरूरत में शामिल होने तालिका मॉडल

  • यह बनाता में attr_accessible अगर आप माता-पिता नियंत्रक में कम से कम एक बच्चे का उदाहरण बनाते हैं तो जीवन आसान हो जाता है।

contributor.rb मॉडल

class Contributor < ActiveRecord::Base 
    attr_accessible #nothing relevant 
    has_many :contributors_isbns 
    has_many :isbns, :through => :contributors_isbns 

isbn.rb मॉडल

class Isbn < ActiveRecord::Base 
    attr_accessible :contributors_attributes, :contributor_id, :istc_id #etc 
    belongs_to :istc 
    has_many :contributors, :through => :contributors_isbns 
    has_many :contributors_isbns 
    accepts_nested_attributes_for :contributors #if you omit this you get a missing block error 

contributors_isbn मॉडल

class ContributorsIsbn < ActiveRecord::Base 
    belongs_to :isbn 
    belongs_to :contributor 
    attr_accessible :isbn_id, :contributor_id 

isbn नियंत्रक

def new 
    @isbn = Isbn.new 
    @title = "Create new ISBN" 
    1.times {@isbn.contributors.build} 
    end 

new.html.erb

<td class="main"> 
<%= nested_form_for @isbn, :validate => false do |f| %> 
<h1>Create new ISBN</h1> 
<%= render 'shared/error_messages', :object => f.object %> 
<%= render 'fields', :f => f %> 
    <div class="actions"> 
    <%= f.submit "Create" %> 
    </div> 

<% end %> 

_fields.html.erb

<%= field_set_tag 'Identifier Details' do %> 

<li> 
<%= f.label 'Title prefix' %> 
<%= f.text_field :descriptivedetail_titledetail_titleelement_titleprefix %> 
</li> 
<li> 
<%= f.label 'Title without prefix' %> 
<%= f.text_field :descriptivedetail_titledetail_titleelement_titlewithoutprefix %> 
</li> 
<li> 
<%= f.label 'ISTC' %> 
<%= f.collection_select(:istc_id, Istc.all, :id, :title_title_text, :prompt => true) %> 
</li> 

<% end %> 


<%= field_set_tag 'Contributor' do %> 
<li> 
<%= f.label 'Contributor Sequence Number' %> 
<%= f.text_field :descriptivedetail_contributor_sequencenumber%> 
</li> 

<%= f.fields_for :contributors, :validate => false do |contributor_form| %> 
<li> 
<%= contributor_form.label :personnameinverted, 'Contributor Name' %> 
<%= contributor_form.collection_select(:isbn_id, Contributor.all, :id, :personnameinverted) %> 
</li> 
<%= contributor_form.link_to_remove "[-] Remove this contributor"%> 
<% end %> 
<%= f.link_to_add "[+] Add a contributor", :contributors %> 


<li> 
<%= f.label 'Contributor Role' %> 
<%= f.text_field :descriptivedetail_contributor_contributorrole %> 
</li> 

<% end %> 
संबंधित मुद्दे