在 Rails、Carrierwave 和 Mongoid 中上传图像时出现问题

发布于 2024-12-03 18:46:47 字数 2400 浏览 1 评论 0原文

我在使用 Rails 和 Carrierwave 将图像上传到 mongodb 时遇到问题。这是我收到的错误消息

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 会处理这个文件处理,但我不确定错过了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

楠木可依 2024-12-10 18:46:47

问题是您需要更新通过调用 mount_uploader 定义的 :image 属性,而不是调用 mount_uploader 的 Image 模型。换句话说,您需要更新 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 ****

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 ****
与他有关 2024-12-10 18:46:47

我必须承认我不熟悉nested_form_for。另外,调试示例会有点令人困惑,因为两个模型都有 :name 和 :image 作为字段。

在我的示例中,一个项目有_很多张照片。在新视图中,我要求一个项目有一张照片。我认为 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 %>

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