我遇到了使用rails和carrierwave將圖片上傳到mongodb的問題。這是錯誤消息我收到在rails,carrierwave和mongoid中上傳圖片時出現問題
Cannot serialize an object of class ActionDispatch::Http::UploadedFile into BSON.
我的模型看起來像這樣
class Offer
include Mongoid::Document
include Mongoid::Timestamps
field :name, :type => String
field :desc, :type => String
field :expiry, :type => Date
has_one :image, as: :viewable
embeds_many :locations, as: :locatable
end
和
class Image
include Mongoid::Document
include Mongoid::Timestamps
field :name, :type => String
field :extension, :type => String
mount_uploader :image, ImageUploader
belongs_to :viewable, polymorphic: true
end
和視圖是
<%= nested_form_for @offer, :html => {:method => :post, :id => "offerNew", :class => "uniForm", :multipart => true} do |f| %>
<fieldset class="inlineLabels" id="setBiz">
<div class="ctrlHolder">
<%= f.label :name, mark_mandatory("Offer Name") %>
<%= f.text_field :name, :class => "textInput large" %>
<p class="formHint">Name of the offer to be displayed in ads ex:5% offer on Panasonic vierra LCD </p>
</div>
<div class="ctrlHolder">
<%= f.label :image, "Image" %>
<%= f.file_field :image %>
<p class="formHint">Upload image </p>
</div>
</fieldset>
<% end %>
這些都是發送到控制器
"utf8"=>"âœ「",
"authenticity_token"=>"8GhVGBg2zTI9FQwZmnKiZtBmgM5DiaQmPZaUIhNeF6I=",
"offer"=>{"catid"=>"4e590231b356f8015f000030",
"name"=>"aaaaaaaaaaaaaaaaaaaa",
"expiry"=>"22/09/2011",
"image"=>#<ActionDispatch::Http::UploadedFile:0xb412e30 @original_filename="51MF4101AA000.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"offer[image]\"; filename=\"51MF4101AA000.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:/tmp/RackMultipart20110908-2893-1a8yqe2>>,
"loc"=>[80.22167450000006,
13.0454044],
}}
的參數,但它是另一種形式的工作得很好,我手動創建一個這樣
params[:images].each do |img|
image = Image.new(:extension => img.content_type.split('/')[1])
image.image = img
@business.images << image
end
我想carrierwave會照顧這個文件處理的圖像文件,但我不確定什麼是錯過的?
是的,這是正確的..謝謝 – RameshVel