0

我认为一切都很好..但它不起作用..
我将整个文件上传到 github
https://github.com/iom00/action2.git
我最近更新了 gem。但是在rails 3.2上也有同样的问题..
请帮帮我~!


投资组合模型

class Portf
  include Mongoid::Document
  field :title, type: String
  field :decs, type: String  

  attr_accessible :images
  embeds_many :images
  accepts_nested_attributes_for :images, :allow_destroy => true

end


图像模型

class Image
  include Mongoid::Document
  include Mongoid::Paperclip

  field :portf_id, type: Integer
  embedded_in :portf , :inverse_of => :images
  has_mongoid_attached_file :file
end

投资组合控制器

  # GET /portfs/new
  # GET /portfs/new.json
  def new
    @portf = Portf.new
    5.times { @portf.images.build }

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @portf }
    end
  end

  # GET /portfs/1/edit
  def edit
    @portf = Portf.find(params[:id])
    5.times { @portf.images.build }
  end

形式

  <%= form_for @portf, :html => { :multipart => true } do |f| %>
    <%= f.fields_for :images do |image| %>
             <% if image.object.new_record? %>
                   <%= image.file_field :file %>                                  
             <% end %>
   <% end %>
4

1 回答 1

1

首先,您需要使images_attributes批量分配可用,而不是images因此您必须这样做

  attr_accessible :images_attributes

您可能还想在:title那里添加。

此外,当您在嵌入式文档中有回形针附件时,您需要添加级联回调。来自https://github.com/meskyanichi/mongoid-paperclip

关于嵌入文档的注意事项:如果您打算保存或更新父文档,则必须将 cascade_callbacks: true 添加到您的 embeds_XXX 语句中。否则,您的数据将被更新,但回形针功能不会运行来复制/更新您的文件。

所以你需要这样做:

  embeds_many :images, :cascade_callbacks => true

您可以在此处阅读有关级联回调的更多信息:http: //mongoid.org/en/mongoid/docs/relations.html#common

另外 - 现在 mongoid-paperclip 存在阻塞问题https://github.com/meskyanichi/mongoid-paperclip/issues/32拉取请求在那里,但尚未合并。因此,如果您将它与 Mongoid 3 一起使用,您可能会偶然发现此错误。

于 2012-08-25T19:17:01.180 回答