0

Rails3 では、Item と Upload の 2 つのモデルを定義しました。アイテムには、ポリモーフィックな関連付けを持つ多数のアップロードがあります。

定義は次のようになります。

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

アップロード属性を使用してアイテムを作成しようとすると、検証が失敗するため失敗します。私の定義に問題はありますか?

4

1 に答える 1

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)

于 2013-08-25T09:57:18.957 に答える