English 中文(简体)
铁路3, mongoid, 承运人wave, 封闭式物体
原标题:Rails 3, mongoid, carrierwave, nested object form

Im利用承运人wave将照片上载到世界模型。 我似乎无法得到上载表格:

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"
Content-Type: image/jpeg
",
 @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">

但是,我不敢确定如何创造实现这一目的的形式。

问题回答

我有(以你的名义):

 <%= 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

页: 1

另外,由于存在违约情况,因此没有必要为嵌入式文件设置accepts_nested_attributes_for

10.times{@world.photos.build} 世界控制器和你将获得10个有正确名称的输入场,请参阅Radar @irc.freenode.net。 # RubyOnRails





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签