Rails 3、mongoid、载波、嵌套对象形式

发布于 2024-12-05 05:10:08 字数 1422 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦笑流年记忆 2024-12-12 05:10:08

我有这个(带有你的名字):

 <%= form_for @world, :multipart => true do |f| %>
   <%= f.fields_for 'photos[0]' do |attachments| %>
     <%= attachments.file_field :image %>
   <% end %>
 <% end %>

这给出了所需的格式。如果您事先不知道要上传多少图像,您可以使用 javascript 增加索引。

然后你可能只是在你的控制器中有:

@world = World.new(params[:world])

但不要忘记你的模型中的这一行:

embeds_many :photos, cascade_callbacks: true

根据 这个问题。

此外,对于嵌入文档使用 accepts_nested_attributes_for 是多余的,因为这是默认设置。

I have this (with your names):

 <%= form_for @world, :multipart => true do |f| %>
   <%= f.fields_for 'photos[0]' do |attachments| %>
     <%= attachments.file_field :image %>
   <% end %>
 <% end %>

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:

@world = World.new(params[:world])

But don't forget this line in your model:

embeds_many :photos, cascade_callbacks: true

as per this issue.

Also, it is redundant to have accepts_nested_attributes_for for an embedded document as this is the default.

绝對不後悔。 2024-12-12 05:10:08

在您的世界控制器中输入 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 to Radar @ irc.freenode.net #RubyOnRails

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文