好的,经过一番挖掘后,我找到了解决方案。首先,这是设置。
模型
class Machine < ActiveRecord::Base
has_many :parts
accepts_nested_attributes_for :parts
end
class Part < ActiveRecord::Base
belongs_to :machine
end
表格
= simple_form_for @machine do |m|
..... A bunch of inputs left out for brevity...
= m.simple_fields_for :parts do |p|
= p.input :name
= p.input :location
= p.input :price
= p.file_field :image
在原始机器控制器中 - machine_posting_params
def machine_posting_params
params.require(:machine).permit(:name, part_attributes: [:name, :location, :price, :image])
end
在工作的机器控制器中 - machine_posting_params
def machine_posting_params
params.require(:machine).permit(:name, part_attributes: [:name, :location, :price, :image, :id])
end
我找不到任何文档的问题是 :id 正在通过我相信carrierwave 传递到part_attributes。当 part_attributes 没有考虑到它时,它会导致carrierwave 在更新时转储图像参考。
因此,如果您有一个嵌套表单并且您的图像在更新时不断消失,请尝试将 :id 添加到 xxxxx_attributs。它适用于 Carrierwave,但我认为这对 Paperclip 不起作用。如果我有时间,我会切换并回来报告。