一千萬個為什麽

搜索

accepts_nested_attributes

我正在嘗試使用附加到另一個模型的評論樣式模型但我不斷收到錯誤:

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

這在Review模型中

  belongs_to :reviewable, :polymorphic => true

有任何想法嗎?

我有一些完全像這樣的照片工作......也是多態的,工作得很好。我能看到的唯一區別是我嵌套了這個模型,所以表單如下所示:

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

我也註意到在我有這個例子的例子中,輸出看起來像這樣:

{"article"=>{"photos_attributes"=>{"1271403114089"=>{"data"=>#}}, "title"=>"dsfdfs", "excerpt"=>"", "content"=>"
\r\n\tsdfsdf

\r\n", "tag_list"=>"lala,lah"}, "commit"=>"Save", "action"=>"create", "controller"=>"articles"}

與沒有的相比:

{"commit"=>"Submit",
 "destination_id"=>"1461",
 "place"=>{"name"=>"sdfasdf",
 "address"=>"asdfasdf",
 "photos"=>{"data"=>#},
 "lng"=>"",
 "website"=>"asdfasdfs",
 "reviews"=>{"content"=>"asdfasdf"},
 "city_id"=>"1461",
 "place_type"=>"1",
 "lat"=>""}}

即一個人有MODELNAME_attributes vs MODELNAME ......我不知道為什麽?表格似乎與嵌套的一個例外相同?

最佳答案

確保使用 fields_for 來獲取嵌套對象的表單構建器。

轉載註明原文: accepts_nested_attributes

推薦教程

熱門教程