我具有以下三个模型和关联:
class ParentProduct < ApplicationRecord
has_many :child_products
accepts_nested_attributes_for :child_products, allow_destroy: true
has_many :attachments, dependent: :destroy
accepts_nested_attributes_for :attachments, allow_destroy: true
end
class ChildProduct < ApplicationRecord
belongs_to :parent_product
has_many :attachments, dependent: :destroy
accepts_nested_attributes_for :attachments, allow_destroy: true
end
class Attachment < ApplicationRecord
belongs_to :parent_product, optional: true
belongs_to :child_product, optional: true
mount_uploader :image, AttachmentUploader
end
此在控制器中:
def new
@parent_product = ParentProduct.new
8.times{ @parent_product.attachments.build }
end
def create
@parent_product = ParentProduct.new(product_params)
respond_to do |format|
if @parent_product.save
flash[:success] = "Product was successfully added."
format.html { redirect_to product_index_path }
format.json { render :new, status: :created, location: @parent_product }
else
format.html { render :new }
format.json { render json: @parent_product.errors, status: :unprocessable_entity }
end
end
end
def product_params
params.require(:parent_product).permit(:name, :price, :color, :size, :description, :subcategory_id,
attachments_attributes: [:id, :image, :parent_product_id, :child_product_id],
child_products_attributes: [:price, :color, :size, :parent_product_id)
end
我使用以下格式来创建父产品和该父产品的附件。同样,使用相同的表格,我可以创建许多子产品(以及每个子产品的附件)并将它们与此父产品相关联:
<%= simple_form_for @parent_product, url: parent_product_path, method: :post, validate: true do |f| %>
<%= f.simple_fields_for :attachments do |attachment| %>
<%= render 'parent_products/attachment_fields', form: attachment %>
<% end %>
<%= f.simple_fields_for :child_products do |child_product| %>
<%= render 'parent_products/child_product_fields', form: child_product %>
<% end %>
<div class="links float-right" style="margin-bottom: 30px;">
<%= link_to_add_association raw('<i class="far fa-plus-square"></i> Add a product variation'),
f, :child_products, form_name: 'form',
wrap_object: Proc.new {|child| 8.times {child.attachments.build }; child },
style: "color: grey; font-size: 14px;" %>
</div>
<% end %>
如您在上面看到的,我正在将wrap_object: Proc.new {|child| 8.times {child.attachments.build }; child }
用作child_products nested form
,以便在单击并添加该嵌套表单时创建8个附件字段。
在child_products nested form
内,我还有另一种嵌套形式,以便为子产品创建附件。
<%= form.simple_fields_for :attachments do |attachment| %>
<%= render 'parent_products/attachment_fields', form: attachment %>
<% end %>
attachment_fields
的部分仅具有此输入:
<%= form.input :image, label: false, as: :file, input_html: %>
唯一的问题是,当我提交表单并且由于某种原因(验证错误)而再次呈现该表单时,这8个附件字段消失了。关于如何解决此问题的任何想法吗?
我现在看到了问题!不允许使用child_products
的附件。
在product_params
中,您现在有:
def product_params
params.require(:parent_product).permit(:name, :price, :color, :size, :description, :subcategory_id,
attachments_attributes: [:id, :image, :parent_product_id, :child_product_id],
child_products_attributes: [:price, :color, :size, :parent_product_id)
end
并且应该(为了清楚起见,对其进行了重新格式化)
def product_params
params.require(:parent_product).permit(
:name, :price, :color, :size, :description, :subcategory_id,
attachments_attributes: [:id, :image, :parent_product_id, :child_product_id],
child_products_attributes: [
:price, :color, :size, :parent_product_id,
attachments_attributes: [:id, :image, :parent_product_id, :child_product_id]
])
end
您还应该在development.log
中看到一条警告,指出该参数已被阻止。