2011-06-10 16 views
2

無法找到爲什麼我在嘗試使用Mongoid和Paperclip上傳文件時遇到此問題。當使用mongoid-paperclip gem進行上傳時未定義的方法「元數據」

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

我跑以下(最新回形針,mongoid,回形針和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" 

我見過的地方,建議增加以下內容的初始化對於出現的東西相似。我已經這樣做了,但無濟於事。

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

其他人遇到這個?

+0

我一直在得到這個並解決它(但與carrierwave),但我從來沒有記得如何。你能向我們展示你的模型,它保存的圖像以及它從上傳的形式。請稍等。 – mraaroncruz 2011-08-29 09:29:09

回答

0

正如我上面所說,我有一個Mongoid類似的問題,Carrierwave和GridFS。
我的解決方案是超級哈克,但它爲我工作。
我有一個圖片類,這是在我的圖像安裝

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 

我carrierwave上傳想發送給它安裝上載(圖像)的關鍵屬性。

Image.create(:image => image_attributes) 

但文章的新建/編輯表單創建的東西,看起來像:

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

,而不是

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

所以我的解決辦法是在形式改變字段的名稱到

file_field :article, :photo 

,然後將照片添加二傳手到創建圖像

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

我像想這文章類=但它無限遞歸和做邪惡的事情。
我也試過這個

file_field "article[image]", :image 

沒有二傳手,並沒有引發異常,但它也沒有保存我的形象。

據我所知,回形針在這些方面非常相似。也許這會爲別人工作還是有人能收拾我的爛攤子

1

我有上傳:

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

這是包含圖像,比如我的所有類使用:

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

然後在窗體看起來像這樣:

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

我認爲這是一個非常簡潔的方式來處理親blem。

+0

不應該是'<%= f.fields_for:logo do | u |%>'而不是? – Annie 2013-07-14 11:37:15

相關問題