2012-08-24 28 views
0

我覺得一切都很好..但它不工作..
我上傳的GitHub上整個文件
https://github.com/iom00/action2.git
我最近更新寶石。但它在導軌3.2上也有同樣的問題..
Plz help me〜!


一切都很好,但回形針,mongoid多圖片上傳不起作用

組合模型

class Portf 
    include Mongoid::Document 
    field :title, type: String 
    field :decs, type: String 

    attr_accessible :images 
    embeds_many :images 
    accepts_nested_attributes_for :images, :allow_destroy => true 

end 


圖像模型

class Image 
    include Mongoid::Document 
    include Mongoid::Paperclip 

    field :portf_id, type: Integer 
    embedded_in :portf , :inverse_of => :images 
    has_mongoid_attached_file :file 
end 

組合控制器

# GET /portfs/new 
    # GET /portfs/new.json 
    def new 
    @portf = Portf.new 
    5.times { @portf.images.build } 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @portf } 
    end 
    end 

    # GET /portfs/1/edit 
    def edit 
    @portf = Portf.find(params[:id]) 
    5.times { @portf.images.build } 
    end 

形式

<%= form_for @portf, :html => { :multipart => true } do |f| %> 
    <%= f.fields_for :images do |image| %> 
      <% if image.object.new_record? %> 
        <%= image.file_field :file %>         
      <% end %> 
    <% end %> 

回答

1

首先,你需要做的可用於大規模分配,而不是imagesimages_attributes因此,你需要做的

attr_accessible :images_attributes 

您可能還需要那裏也加上:title

此外,當您在嵌入式文檔中有回形針附件時,您需要添加級聯回調。從https://github.com/meskyanichi/mongoid-paperclip

上嵌入文檔注意:如果你打算保存或更新父文檔,您必須添加cascade_callbacks:忠於你embeds_XXX聲明。否則,您的數據將被更新,但回形針功能將不會運行以複製/更新您的文件。

所以,你需要做的:

embeds_many :images, :cascade_callbacks => true 

你可以閱讀更多關於此級聯回調:也http://mongoid.org/en/mongoid/docs/relations.html#common

- 有一個攔截器問題與mongoid,回形針,現在https://github.com/meskyanichi/mongoid-paperclip/issues/32拉入請求在那裏,但它尚未合併。所以你可能偶然發現這個錯誤,如果你使用它與Mongoid 3.

+0

Omg ..非常感謝你!你的天使〜! – user1320189

+0

是否解決了這個問題?!?! – user1320189