我正在使用carrierwave將照片上傳到世界模型。我似乎無法獲得上傳表單權:Rails 3,mongoid,載波,嵌套對象形式
class World
include Mongoid::Document
embeds_many :photos
accepts_nested_attributes_for :photos
end
class Photo
include Mongoid::Document
mount_uploader :image, WorldPhotoUploader
embedded_in :world
end
# show.haml
= form_for world, :html => {:multipart => true} do |f|
= f.fields_for world.photos.build do |photo|
= photo.file_field :image
這給了我這種輸入形式:
<input id="world_photo_image" name="world[photo][image]" type="file">
不工作,我得到
Cannot serialize an object of class ActionDispatch::Http::UploadedFile into BSON.
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"e2PzZlSY0NwiCqDWn7ZMNwqnypP+GC23PcMuy+uGyF0=",
"world"=>{"photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00000103182ac8 @original_filename="Black Box fish.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"world[photo][image]\"; filename=\"Black Box fish.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:/var/folders/IY/IY7PGAf2F9OD6CIKr1RQo++++TI/-Tmp-/RackMultipart20110917-57084-zwoyfy>>}},
"commit"=>"Upload",
"id"=>"pluto"}
輸入這似乎工作是:
<input id="world_photo_image" name="world[photos][][image]" type="file">
Bu T I我不知道如何創建的形式來獲取
添加'cascade_callbacks:true'解決了我的問題。 –