【问题标题】:Mongomapper parent model validation fails with accept_nested_attributes_forMongoapper 父模型验证因 accept_nested_attributes_for 而失败
【发布时间】:2013-07-04 14:45:29
【问题描述】:

在 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 属性创建项目时,它会失败,因为验证失败。 我的定义有问题吗?

【问题讨论】:

    标签: ruby-on-rails ruby mongomapper


    【解决方案1】:

    您是如何尝试创建该项目的?向我们展示命令。

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

    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)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 2012-09-07
      • 2020-03-15
      相关资源
      最近更新 更多