rails 3,paperclip(&formtastic) – 删除图像附件

我似乎无法找到所有组件中完整的示例。 我很难删除图片附件

  1. class Product has_many :product_images, :dependent => :destroy accepts_nested_attributes_for :product_images end class ProductImage belongs_to :product has_attached_file :image #(etc) end 
  2. 视图

       {:multipart => true} do |f| %>     :boolean, :label => image_tag(product_image.object.image.url(:thumb)) %>   :file, :name => "Add Image" %>     
  3. 调节器

      class Admin::ProductsController  'edit' end end end 

看起来不错,但是,当我选中复选框时,字面上没有任何反应。 在请求中我看到:

  "product"=>{"manufacturer_id"=>"2", "size"=>"", "cost"=>"5995.0", "product_images_attributes"=>{"0"=>{"id"=>"2", "_destroy"=>"1"}} 

但没有任何更新,产品图像也不会保存。

我错过了关于’accepts_nested_attributes_for’如何工作的基本信息吗?

来自ActiveRecord :: NestedAttributes :: ClassMethods的API文档

:allow_destroy

如果为true,则使用_destroy键和计算结果为true的值(例如,1,’1,true或’true’)销毁属性哈希中的所有成员。 默认情况下,此选项处于关闭状

所以:

 accepts_nested_attributes_for :product_images, allow_destroy: true