开发者

Undefined Method 'metadata' when uploading using mongoid-paperclip gem

开发者 https://www.devze.com 2023-03-12 16:46 出处:网络
Can\'t find out why I\'m getting this when trying to upload files with Mongoid and Paperclip. undefined method `metadata\' for #<ActionDispatch::Http::UploadedFile:0x10625e930>

Can't find out why I'm getting this when trying to upload files with Mongoid and Paperclip.

undefined method `metadata' for #<ActionDispatch::Http::UploadedFile:0x10625e930>

I'm running the following (latest of paperclip, mongoid-paperclip and aws-s3):

gem "rails", "3.0.6"
gem "mongoid", "2.0.1"
gem "bson_ext", "1.3.0"
gem "paperclip"
gem "mongoid-paperclip", :require => "mongoid_paperclip"
gem "aws-s3",            :require => "aws/s3"

I've seen places recommending adding the following to an initializer for things that appear to be sim开发者_如何学编程ilar. I've done this, but to no avail.

if defined? ActionDispatch::Http::UploadedFile
    ActionDispatch::Http::UploadedFile.send(:include, Paperclip::Upfile)
end

Anyone else encounter this?


I have the the uploader:

class Image
  include Mongoid::Document
  embedded_in :imageable, polymorphic: true
  mount_uploader :file, ImageUploader
end

Which is used in all my classes containing images, such as:

class Shop
  include Mongoid::Document
  embeds_one :logo, as: :imageable, :class_name => 'Image', cascade_callbacks: true  
end

Then in the form looks like this:

<%= form_for @shop do |f| %>
  <%= f.fields_for :cover do |u|%>
    <%= u.file_field :file %>
  <% end %>
  <%= f.submit 'Save' %>
<% end %>

I think this is a pretty neat way to handle the problem.


As I said above, I had a similar problem with Mongoid, Carrierwave and GridFS.
My solution is super hacky but it worked for me.
I had an Image class which is where my image was mounted

class Image
  include Mongoid::Document
  include Mongoid::Timestamps

  field :title
  field :image
  field :order

  mount_uploader :image, ImageUploader
  embedded_in :article
end

class Article
  include Mongoid::Document
  ...
  embeds_one :image
  ...
end

My carrierwave uploader wanted the attributes sent to it with the key of the mount uploader (image).

Image.create( :image => image_attributes)

But the article's new/edit form created something that looked like:

:article => { :image => #ActionDispatch... }

instead of

:article => { :image => { :image => #ActionDispatch... } }

so my solution was to change the name of the field in the form to

file_field :article, :photo

and then add a photo setter to the article class that created an image

model Article
  include Mongoid::Document
  ...
  def photo=(attrs)
    create_image(:image => attrs)
  end
end

I tried this with image= but it infinitely recursed and did evil things.
I also tried this

file_field "article[image]", :image

without the setter and it didn't raise an exception but it also didn't save my image.

as far as i know, paperclip is pretty similar in these regards. Maybe this will work for someone or someone can clean up my mess

0

精彩评论

暂无评论...
验证码 换一张
取 消