2013-02-06 61 views
1

我使用rails 3.2.11,mongoid 3.0.18,carrierwave(0.7.1)和carrierwave-mongoid(0.3.0)當我保存一個嵌套的模型只改變文件字段,保存事件沒有觸發

兩種型號:產品有很多組件。組件具有文件字段。我使用gem nested_form在產品頁面上添加許多組件表單。

class Product 
    include Mongoid::Document 
    include Mongoid::Timestamps 

    field :title 
    field :image 

    embeds_many :components, cascade_callbacks: true 
    accepts_nested_attributes_for :components, allow_destroy: true 
end 

class Component 
    include Mongoid::Document 
    include Mongoid::Timestamps 
    field :title 
    field :author 
    field :file 

    embedded_in :product 

    mount_uploader :file, BaseFileUploader 
end 

控制器:

class ProductsController < BaseController 

    def update 
    resource = Product.find(params[:id]) 
    if resource.update_attributes(params[:product])  
     redirect_to collection_path 
    else 
     resource.components.build unless resource.components # there should be always one component 
     render :edit 
    end 
    end 
end 

當我改變(!這個領域已經有值)只有一個文件中的字段,沒有保存查詢數據庫。

,但是當我在控制器添加強制保存像

class ProductsController < BaseController 

    def update 
    resource = Product.find(params[:id]) 
    if resource.update_attributes(params[:product]) 
     resource.components.each do |component| 
     component.save! 
     end  
     redirect_to collection_path 
    else 
     resource.components.build unless resource.components # there should be always one component 
     render :edit 
    end 
    end 
end 

和它的作品!你知道這裏的原因是什麼嗎?

回答

0

我也看到這個問題。如果我更新了另一個字段以及嵌入式文檔上的文件,則會觸發上傳。如果它只是文件,沒有任何反應。

相關問題