4

我不确定我在这里做错了什么。文件上传正常,但如果我提交表单而不选择要上传的文件,则会删除以前附加的图像。

ActiveAdmin 表单如下所示:

form do |f|
  f.inputs do
    f.input :model_number
    f.input :description
    f.input :slug
    f.input :categories
    f.has_many :product_images do |image|
      image.input :product_id, as: :hidden, id: :product_id, input_html: { value: "%i" }
      image.input :image
    end
  end
  f.actions
end

以及各个型号的相关部分:

class ProductImage < ActiveRecord::Base
  belongs_to :product

  mount_uploader :image, ProductImageUploader

  validates :image, :product_id, presence: true
end

class Product < ActiveRecord::Base
  has_many :product_images, dependent: :destroy

  accepts_nested_attributes_for :product_images

  validates_associated :product_images
end

任何见解将不胜感激。谢谢!

4

1 回答 1

4

It looks like I was a bit overzealous with my validations. Removing product id from the ProductImages validations and simplifying image.input :product_id, as: :hidden, id: :product_id, input_html: { value: "%i" } in the form to image.input :product_id, as: :hidden in the form makes image attach correctly to existing products or new products.

于 2013-11-12T18:25:38.257 回答