accepts_nested_attributes

时间:2010-04-15 17:05:45

标签: ruby-on-rails polymorphic-associations nested-attributes polymorphism

我正在尝试使用附加到另一个模型的评论样式模型,但我一直收到错误:

Review(#2171315060) expected, got Array(#2148226700) 

使用params:

   Processing PlacesController#create (for 127.0.0.1 at 2010-04-15 18:57:02) [POST]
      Parameters: {"commit"=>"Submit", "action"=>"create", "destination_id"=>"3243", "controller"=>"places", "place"=>{"address"=>"sdfsdf", "name"=>"dssfs", "lng"=>"", "reviews"=>{"content"=>"sdfsdfsdfsd"}, "website"=>"sdfsdf", "city_id"=>"3243", "lat"=>"", "place_type"=>"1"}}

我在父模型中有这个

  has_many :reviews, :as => :reviewable, :dependent => :destroy
  accepts_nested_attributes_for :reviews

这在评论模型中

  belongs_to :reviewable, :polymorphic => true

有什么想法吗?

我有一些与照片一样的工作...也是多态的,工作正常。我能看到的唯一区别是我已经嵌套了这个模型,因此表单看起来像这样:

<% form_for :place, :url => destination_places_path(@city), :html => { :multipart => true } do |f| %>

我也注意到,在我的示例中,输出看起来像这样:

{"article"=>{"photos_attributes"=>{"1271403114089"=>{"data"=>#<File:/var/folders/9b/9b3G5CRzGk0okD2UGi-hvE+++TI/-Tmp-/RackMultipart20100416-26553-177jk7b-0>}}, "title"=>"dsfdfs", "excerpt"=>"", "content"=>"<p>\r\n\tsdfsdf</p>\r\n", "tag_list"=>"lala,lah"}, "commit"=>"Save", "action"=>"create", "controller"=>"articles"}

与没有的相比:

{"commit"=>"Submit",
 "destination_id"=>"1461",
 "place"=>{"name"=>"sdfasdf",
 "address"=>"asdfasdf",
 "photos"=>{"data"=>#<File:/var/folders/9b/9b3G5CRzGk0okD2UGi-hvE+++TI/-Tmp-/RackMultipart20100416-26553-8edo05-0>},
 "lng"=>"",
 "website"=>"asdfasdfs",
 "reviews"=>{"content"=>"asdfasdf"},
 "city_id"=>"1461",
 "place_type"=>"1",
 "lat"=>""}}

即。一个人有MODELNAME_attributes vs MODELNAME ...我不知道为什么?表格似乎与嵌套的一个例外相同吗?

1 个答案:

答案 0 :(得分:0)

确保使用fields_for获取嵌套对象的表单构建器。