2011-09-08 54 views
4

我遇到了使用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會照顧這個文件處理的圖像文件,但我不確定什麼是錯過的?

回答

1

的問題是,你需要更新:通過調用定義圖像屬性mount_uploader,而不是圖像模型,其中mount_uploader被稱爲。換句話說,您需要更新Offer.image.image而不是Offer.image。

這應該工作:

**** snip **** 

    <div class="ctrlHolder"> 
    <%= f.fields_for Image.new do |i| %> 
     <%= i.label :image, "Image" %> 
     <%= i.file_field :image %> 
    <%= end %> 
    <p class="formHint">Upload image </p> 
    </div> 

    **** snip **** 
+0

是的,這是正確的..謝謝 – RameshVel

0

我必須承認我對nested_form_for不熟悉。另外,調試你的例子有點混亂,因爲兩個模型都有:name和:image作爲字段。

在我的例子中,一個項目has_many照片。在新視圖中,我需要一個項目有一張照片。我認爲 that fields_for可能會有所作爲?

控制器:

@item = Item.new 
@photo = @item.photos.build 

查看:

<%= form_for(@item, :multipart=>true) do |f| %> 
<h3>Photo</h3> 
<div class="photo"> 
    <%= f.fields_for :photos do |ff| %> 
    <%= ff.file_field :file %> 
<% end %> 
</div> 
<% end %> 
+0

是那混亂的,你可以在圖像文檔改變像場(mount_uploader:圖像,ImageUploader)至(mount_uploader:資產,ImageUploader)..和nested_form_for是一種形式的插件爲創造動態嵌套形式在飛行中...我與常規forms_for驗證太,它不工作:( – RameshVel