2013-07-04 89 views
0

在Rails3中,我定義了2個模型,只是Item和Upload。 項目有許多帶有多態關聯的上傳。Mongomapper父模型驗證失敗,accept_nested_attributes_for

定義看起來象下面這樣:

class Item 
    include MongoMapper::Document 
    include MongoMapper::AcceptsNestedAttributes 

    attr_accessible :uploads_attributes 

    belongs_to :category 
    many :uploads,:as => :picture_of 

    accepts_nested_attributes_for :uploads 


    key :name, String 
    key :description, String 

    validates_presence_of :name 

    timestamps! 
end 


class Upload 
    require 'carrierwave/orm/mongomapper' 
    include MongoMapper::EmbeddedDocument 
    attr_accessible :image,:remote_image_url 

    # belongs to Item, Event 
    # upload , just for photo 
    belongs_to :picture_of, :polymorphic => true 

    key :versions, Array 
    mount_uploader :image, ImageUploader 


    timestamps! 

    # for nested_attributes 
    def _destroy 
    end 
end 

當試圖與上傳屬性來創建項目,它失敗,因爲驗證失敗。 我的定義有問題嗎?

回答

0

你是如何創建該項目?向我們顯示命令。

item = Item.create(...) 
puts item.errors.messages 

但是NestedAttributes不MongoMaper支持[1]。你必須使用第三方插件,對吧?

如果您僅使用NestedAttributes來顯示漂亮的表單,則必須知道它與關係正常工作。

<%= form_for(@item) do |f| %> 
... 
<%= render 'upload_form', uploads: @item.uploads, form_parent: f %> 
... 

<% end %> 

[1] https://groups.google.com/forum/#!topic/mongomapper/6Sw19uIwJoc(2010)