以表格形式构建嵌入式mongoid文档

时间:2012-07-25 12:32:55

标签: ruby-on-rails mongoid

我可能会遗漏一些愚蠢但我无法将多个嵌入式照片文件放入表单

模型

class Product
   include Mongoid::Document

   embeds_many :photos, cascade_callbacks: true
end

class Photo
   include Mongoid::Document

   embedded_in product, :inverse_of => :photos
   field :image_filename
   mount_uploader :image, ImageUploader
end

Controller - products_controller.rb

def new
   @product = Product.new
   3.times { @product.photos.build }
end

表格

然后我用

做表格
fields_for @product.photos do |photo|
   <%= photo.file_field :image %>
end

问题是只有1张照片出现,但我在控制器中构建了3张照片。即使我在内存中构建3,@ product.photos.count的计数也为0。我错过了什么吗?

1 个答案:

答案 0 :(得分:3)

fields_for @product.photos do |photo|
   <%= photo.file_field :image %>
end

应该是

fields_for :photos do |photo|
   <%= photo.file_field :image %>
end