简体   繁体   English

Mongomapper父模型验证失败,并带有accept_nested_attributes_for

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

相关问题 使用accept_nested_attributes_for进行验证 - Validations with accept_nested_attributes_for 尽管存在关系,但Rspec +应该在accept_nested_attributes_for上进行测试失败 - Rspec + shoulda test fails on accept_nested_attributes_for despite relationship has_one,reject_if和accept_nested_attributes_for仍会触发验证 - has_one, reject_if, and accept_nested_attributes_for still triggers validation accept_nested_attributes_for:allow_destroy,:_ destroy无效 - accept_nested_attributes_for :allow_destroy, :_destroy not working 多对多关系中的accept_nested_attributes_for - accept_nested_attributes_for in a many-to-many relationship Rails 2.3.5和Ruby 1.9.1中的accept_nested_attributes_for - accept_nested_attributes_for in Rails 2.3.5 and Ruby 1.9.1 accept_nested_attributes_for在Rails上创建一个额外的nil记录 - accept_nested_attributes_for creates one extra nil record on Rails attr_accessor在accept_nested_attributes_for中无法访问 - attr_accessor not accessible in accept_nested_attributes_for Rails属于具有accept_nested_attributes_for的多个模型 - Rails belongs_to multiple models with accept_nested_attributes_for accept_nested_attributes_for + reject_if +未保存的子对象的显示错误 - accept_nested_attributes_for + reject_if + display error for unsaved child objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM