0

在 Rails3 中,我定义了 2 个模型,只有 Item 和 Upload。项目有许多具有多态关联的上传。

定义如下所示:

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

尝试使用 Uploads 属性创建项目时,由于验证失败,因此失败。我的定义有问题吗?

4

1 回答 1

0

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]. 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.

<%= 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)

于 2013-08-25T09:57:18.957 回答