2010-12-12 8 views
18

これはしばらくの間グーグルで運がありませんでした。 "NoMethodError(未定義メソッド` with_indifferent_access' 。それは属性をネストされたパッシングサービス"NoMethodError(定義されていないメソッド` with_indifferent_access 'for ...)はネストされた属性を渡すとき

他のネストされたものが動作し、彼らはしかし、1対1であり、これは1対多のです...

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!"}, 

を追加するときしかし、それはdate_of_incidentを削除

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

を引き起こし、それだけで次の属性についても同じ文句を言うでしょう。

以前は「incident_attributes」(単数の事件)とhas_many:事件がありましたが、これを変更しても問題はありませんでした。

+0

私は同様の問題が発生しています。しかし、それはhas_many関連のために私にとってはうまくいきます。しかし、has_one関連についても同じエラーが返されます。 has_one関連のアクセス方法を教えてください。 – Ashi

答えて

17

お客様は多くのインシデントが発生しているため、Railsでは「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 | incident_f | %>を働かせます。代わりに:インシデント私は手作業で "incidents_attributes"を書いていました –

+0

+1ハッシュオブジェクトを1対多の関係に挿入しなければならない問題がありました: '... [:addresses_attributes] = params [:address] 'のようになります。代わりに、' '[:addresses_attributes] = {" 0 "=> params [:address]}' – DJTripleThreat