[英]Mongomapper parent model validation fails with accept_nested_attributes_for
In Rails3, I have defined 2 models, just Item and Upload. 在Rails3中,我定义了2个模型,分别是Item和Upload。 Item has many Uploads with polymorphic association.
项目有许多具有多态关联的上载。
The definition looks like below: 定义如下所示:
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
When trying to create Item with Uploads attributes, it fails ,because of Validation fail. 尝试创建具有Uploads属性的项目时,它会失败,因为验证失败。 Is there any problem with my definition?
我的定义有问题吗?
How are you trying to create the item? 您如何尝试创建商品? Show us the command.
向我们显示命令。
item = Item.create(...)
puts item.errors.messages
But NestedAttributes isn't supported in MongoMaper [1]. 但是MongoMaper [1] 不支持NestedAttributes 。 You must be using a third party plugin, right?
您必须使用第三方插件,对吗?
If you are using NestedAttributes only to show nice forms, you must know that it works fine with relations. 如果仅使用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) [1] https://groups.google.com/forum/#!topic/mongomapper/6Sw19uIwJoc(2010 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.