" NoMethodError(未定义的方法`with_indifferent_access“为..."通过它嵌套属性时 [英] "NoMethodError (undefined method `with_indifferent_access' for..." when passing it nested attributes

查看:164
本文介绍了" NoMethodError(未定义的方法`with_indifferent_access“为..."通过它嵌套属性时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

被谷歌搜索这一段时间,有没有运气。我发现了一个NoMethodError(未定义的方法`with_indifferent_access错误在我的Web服务时将它传递嵌套的属性。

Been googling this for a while and having no luck. I'm getting an "NoMethodError (undefined method `with_indifferent_access'" error on my web service when passing it nested attributes.

其它嵌套那些工作,他们是一到1然而,这是一对许多...

Other nested ones work, they are one-to-one however and this is one-to-many...

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]

然而,当添加

However, when adding

"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,它只会抱怨的下一个属性是一样的。

Removing the date_of_incident and it will just complain the same about the next attribute.

previously我有incident_attributes(奇异事件)和的has_many:事件发生,但改变这并没有帮助

Previously I had "incident_attributes" (singular incident) and has_many :incident, but changing this didn't help.

推荐答案

由于客户有许多事件,Rails的预计,incidents_attributes是一个数组,而不是一个哈希值。这的的自动发生,当你这样做:

because customer has many incidents, Rails expects the "incidents_attributes" to be an array, not a hash. This should automatically happen, when you do:

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

这篇关于&QUOT; NoMethodError(未定义的方法`with_indifferent_access“为...&QUOT;通过它嵌套属性时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆