मैं एक साधारण मॉडल है में fields_for का उपयोग करकई स्थानों
class Ad < ActiveRecord::Base
has_many :ad_items
end
class AdItem < ActiveRecord::Base
belongs_to :ad
end
मुझे लगता है कि मुझे नया विज्ञापन बनाने और यह
करने के लिए कुछ आइटम जोड़ने के लिए प्रपत्र से पता चलता एक "विज्ञापन/नई 'दृश्य है, .html.erb कोड की तरह है एक निम्नलिखित:
<% form_for @ad, do |ad_form| %>
<!-- some html -->
<% ad_form.fields_for :ad_items do |f| %>
<%= f.text_area "comment", :class => "comment", :rows => "5" %>
<% end %>
<!-- some other html -->
<% ad_form.fields_for :ad_items do |f| %>
<% render :partial => "detailed_item_settings", :locals => {:f => f} %>
<% end %>
<% end %>
विज्ञापन एक आइटम है जब ...
def new
@ad = session[:user].ads.build
# Create one item for the ad. Another items will be created on the
# client side
@ad.ad_items.build
# standard stuff ...
end
... एचटीएमएल, जिसके परिणामस्वरूप इस तरह दिखेगा: यह कोड में कहा गया है के रूप में
<form ... >
<!-- some html -->
<textarea id="ad_items_attributes_0_comment" name="ad[ad_items_attributes][0][comment]" />
<!-- some other html -->
<!-- "detailed_item_settings" partial's content -->
<textarea id="ad_ad_items_attributes_1_desc" name="ad[ad_items_attributes][1][desc]" />
<!-- end -->
</form>
, मैं का उपयोग fields_for विधि दो बार, HTML संरचना की वजह से, कि मैं का पालन करना होगा
दूसरे "फ़ील्ड_for" कॉल के लिए, "आइटम" के लिए इंडेक्स पहले से 1 है, 0 नहीं, जैसा कि मुझे उम्मीद है।
ऐसा नहीं है कि आप को "fields_for" विधि द्वारा, कुछ आंतरिक काउंटर की संख्या बढ़ जाएगा ...
की तरह है, लेकिन यह एक छोटे से अजीब व्यवहार है ...
मैं स्थापित करने के लिए कोशिश की है: सूचकांक => 0 फ़ील्ड_for के लिए, लेकिन सभी एक ही रहता है ...
यहां क्या गलत है?
धन्यवाद, आदमी! : child_index कुंजी था! – AntonAL