Question

I am having problem uploading image to mongodb using rails and carrierwave. This is the error message i am getting

Cannot serialize an object of class ActionDispatch::Http::UploadedFile into BSON.

My model looks like this

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

and

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

And the view is

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

these are the parameters sent to controller

"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],
}}

But it was working fine in another form where i manually create the image document like this

params[:images].each do |img|
        image = Image.new(:extension => img.content_type.split('/')[1])
        image.image = img
        @business.images << image
end

I thought carrierwave would take care of this file handling, but i am unsure what is missed?

Was it helpful?

Solution

The problem is that you need to update the :image attribute defined by the call to mount_uploader, not the Image model in which mount_uploader is called. In other words, you need to update Offer.image.image instead of Offer.image.

This should work:

   **** 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 ****

OTHER TIPS

I must admit I am unfamiliar with nested_form_for. Also, it is slightly confusing to debug your example, because both models have :name and :image as fields.

In my example, an Item has_many photos. In the new view, I require an Item have one photo. I think that that fields_for might make the difference?

Controller:

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

View:

<%= 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 %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top