Rails 3、mongoid、载波、嵌套对象形式
我正在使用 Carrierwave 将照片上传到世界模型。我似乎无法正确获取上传表单:
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">
但我不确定如何创建表单来获取该表单
I'm using carrierwave to upload photos to a World model. I can't seem to get the upload form right:
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
This gives me this form input:
<input id="world_photo_image" name="world[photo][image]" type="file">
Which doesn't work, I get
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"}
The input that seems to work is:
<input id="world_photo_image" name="world[photos][][image]" type="file">
But i'm not sure how to create the form to get that
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有这个(带有你的名字):
这给出了所需的格式。如果您事先不知道要上传多少图像,您可以使用 javascript 增加索引。
然后你可能只是在你的控制器中有:
但不要忘记你的模型中的这一行:
根据 这个问题。
此外,对于嵌入文档使用
accepts_nested_attributes_for
是多余的,因为这是默认设置。I have this (with your names):
This gives the desired format. You can increase the index with javascript if you don't know beforehand how many images are going to be uploaded.
Then you may simply have in you controller:
But don't forget this line in your model:
as per this issue.
Also, it is redundant to have
accepts_nested_attributes_for
for an embedded document as this is the default.在您的世界控制器中输入
10.times {@world.photos.build}
,您将获得 10 个具有正确名称的输入字段,感谢Radar @ irc.freenode.net #RubyOnRails
10.times {@world.photos.build}
in your World controller and you will get 10 input fields with correct names, thanks goes toRadar @ irc.freenode.net #RubyOnRails