0
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

I am using a multi-model form for post request of a new item (with review). I am using following parameters in post request:

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

But I got following error while saving:

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

I suspect array in item_reviews as the culprit, so I did the following:

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

but then I started getting following error:

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

How can I solve it?

Akash Agrawal
  • 4,711
  • 5
  • 28
  • 26
  • What does your form look like? `item_reviews` should be `item_reviews_attributes` if you are using Rails `anaf`. – nowk Aug 26 '12 at 16:01
  • @kwon sorry for the late reply. For forms, I have followed answer: http://stackoverflow.com/questions/12087735/create-new-object-in-rails3-using-multimodel-form Please don't miss my comments in above answer. – Akash Agrawal Aug 29 '12 at 10:48

0 Answers0