HashWithIndifferentAccess在post请求多对一关系时

时间:2012-08-26 04:49:01

标签: ruby-on-rails-3 activerecord multi-model-forms

class Item < ActiveRecord::Base
  attr_accessible :name, :item_reviews
  has_many :item_reviews, :dependent => :destroy
  accepts_nested_attributes_for :item_reviews
end

class ItemReview < ActiveRecord::Base
  attr_accessible :price, :value_for_money
  belongs_to :item
end

我正在使用多模型表单来发布新项目的请求(带有评论)。我在帖子请求中使用以下参数:

{"utf8"=>"✓",
 "authenticity_token"=>"oZZ6T5bxWHnSiO2Tdz3eFUVCrRH3lzzxdBpuJjlWcho=",
 "item"=>{"name"=>"test",
 "item_reviews"=>[{"value_for_money"=>"1",
 "price"=>"25000"}]},
 "commit"=>"Submit"}

但是我在保存时遇到了以下错误:

ItemReview(#89032230) expected, got ActiveSupport::HashWithIndifferentAccess(#77848120)

我怀疑item_reviews中的数组是罪魁祸首,所以我做了以下几点:

params[:item][:item_reviews] =  params[:item][:item_reviews][0]

然后我开始收到以下错误:

ItemReview(#87446700) expected, got Array(#75744590)

我该如何解决?

0 个答案:

没有答案