accepts_nested_attributes
I'm trying to use a comment style model which is attached to another model but I keep getting the error:
Review(#2171315060) expected, got Array(#2148226700)
With 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"}}
I have this in the parent model
has_many :reviews, :as => :reviewable, :dependent => :destroy
accepts_nested_attributes_for :reviews
And this in the Review model
belongs_to :reviewable, :polymorphic => true
Any ideas?
I have something exactly like this working with photos... also polymorphic which works fine. The only difference I can see is that I've nested this model, so the form looks like this:
<% form_for :place, :url => destination_places_path(@city), :html => { :multipart => true } do |f| %>
I've also noticed that in the example I have that works, the output looks like this:
{"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"}
Compared to the one that doesn't:
{"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"=>""}}
I.e. one has MODELNAME_attributes vs MODELNAME.开发者_JAVA技巧.. and i'm not sure why? The form seems to be the same with the one exception of the nesting?
Make sure you are using fields_for
to get a form builder for the nested object(s).
精彩评论