2010-12-12 7 views
18

थोड़ी देर के लिए इसे गुगल कर रहा है और कोई भाग्य नहीं है। मुझे "NoMethodError (अपरिभाषित विधि 'with_indifferent_access'" त्रुटि हो रही है । सेवा जब यह नेस्टेड विशेषताओं गुजर"NoMethodError (अपरिभाषित विधि 'with_indifferent_access' ..." जब इसे नेस्टेड विशेषताओं को पास करते हैं

अन्य नेस्टेड वाले काम करते हैं, वे एक-से-एक तथापि हैं और यह एक-से-कई है ...

class Customer < ActiveRecord::Base 

    belongs_to :quote 

    has_one :policy, :dependent => :destroy 
    has_one :vehicle, :dependent => :destroy 
    has_many :claims, :dependent => :destroy 
    has_many :incidents, :dependent => :destroy 

    accepts_nested_attributes_for :policy 
    accepts_nested_attributes_for :vehicle 
    accepts_nested_attributes_for :incidents 
end 

सही ढंग से कार्य करना:

Processing QuotesController#create to xml (for 127.0.0.1 at 2010-12-12 15:16:33) [POST] 
    Parameters: {"quote"=>{"customer_attributes"=>{"address1"=>nil, "city"=>"ggh", "dob"=>nil, "address2"=>nil, "title"=>nil, "country"=>nil, "postcode"=>nil, "vehicle_attributes"=>{"mileage"=>nil, "registration"=>nil, "value"=>nil, "parking"=>nil}, "policy_attributes"=>{"breakdown"=>nil, "windscreen"=>"1", "excess"=>nil}, "telephone"=>nil, "surname"=>nil, "forename"=>"wasdddfggh", "email"=>nil}}} 
    [4;35;1mQuote Create (1.0ms)[0m [0mINSERT INTO "quotes" ("created_at", "updated_at") VALUES('2010-12-12 15:16:33', '2010-12-12 15:16:33')[0m 
    [4;36;1mCustomer Create (0.0ms)[0m [0;1mINSERT INTO "customers" ("address1", "city", "dob", "address2", "quote_id", "created_at", "title", "country", "postcode", "updated_at", "telephone", "forename", "surname", "email") VALUES(NULL, 'ggh', NULL, NULL, 2, '2010-12-12 15:16:33', NULL, NULL, NULL, '2010-12-12 15:16:33', NULL, 'wasdddfggh', NULL, NULL)[0m 
    [4;35;1mPolicy Create (0.0ms)[0m [0mINSERT INTO "policies" ("breakdown", "created_at", "windscreen", "excess", "updated_at", "customer_id") VALUES(NULL, '2010-12-12 15:16:33', 1, NULL, '2010-12-12 15:16:33', 2)[0m 
    [4;36;1mVehicle Create (0.0ms)[0m [0;1mINSERT INTO "vehicles" ("mileage", "created_at", "updated_at", "registration", "value", "parking", "customer_id") VALUES(NULL, '2010-12-12 15:16:33', '2010-12-12 15:16:33', NULL, NULL, NULL, 2)[0m 
Completed in 153ms (View: 4, DB: 1) | 201 Created [http://localhost/quotes.xml] 
host/quotes.xml] 

हालांकि, जब

"incidents_attributes"=>{"date_of_incident"=>"2008-05-19", "sum_of_claim"=>"34554", "description"=>"PLEASE!"}, 

जोड़ने यह

NoMethodError (undefined method `with_indifferent_access' for "2008-05-19":String): 

का कारण बनता है date_of_incident निकाला जा रहा है और यह सिर्फ अगले विशेषता के बारे में एक ही शिकायत करेंगे।

पहले मैंने "event_attributes" (एकवचन घटना) और has_many: घटना थी, लेकिन इसे बदलने से मदद नहीं मिली।

+0

मैं एक ऐसी ही समस्या हो रही है। लेकिन यह है_मैनी एसोसिएशन के लिए मेरे लिए ठीक काम कर रहा है। लेकिन has_one एसोसिएशन के लिए एक ही त्रुटि देता है। क्या आप कृपया बता सकते हैं कि has_one एसोसिएशन के लिए कैसे पहुंचे? – Ashi

उत्तर

17

क्योंकि ग्राहक की कई घटनाएं हैं, रेल उम्मीद करता है कि "incidents_attributes" एक सरणी है, न कि हैश। यह अपने आप ही हो जाना चाहिए, जब आप कार्य करें:

<%= form_for @customer do |f| %> 
    <%= f.fields_for :incidents do |incident_fields| %> 
    <%= incident_fields.date_select :date_of_incident %> 
    <% end %> 
<% end %> 
+1

क्योंकि फ़ॉर्म एक अलग प्रोजेक्ट में है, (सक्रिय संसाधन का उपयोग करके) मुझे <% customer_f.fields_for नहीं मिल सका: incedents do | event_f | %> काम करने के लिए। तो इसके बजाय: घटनाएं मैं मैन्युअल रूप से "incidents_attributes" लिख रहा हूं –

+0

+1 मुझे एक समस्या थी जहां मुझे एक से अधिक रिश्तों में एक हैश ऑब्जेक्ट डालना पड़ा था: '... [: address_attributes] = पैराम्स [: पता] 'और इसके बजाय यह काम करता है:' ... [: address_attributes] = {"0" => पैराम्स [: पता]} ' – DJTripleThreat

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