Rails cocoon:提交表单后嵌套字段消失
Rails cocoon: Nested fields disappear after I submit the form
我有以下三个模型和关联:
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
中看到一条警告,指出参数已被阻止。
我有以下三个模型和关联:
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
中看到一条警告,指出参数已被阻止。