循环中的嵌套形式
Nested form in a loop
我有一个 Inquiry
,它有很多 InquiryItem
模型,它有很多 InquiryItemPrice
。
在我的显示视图中,我正在遍历我的 inquiry_items
,对于其中的每一个,我都需要 inquiry_item_price
.
的输入字段
这些是我的模型:
询价
class Inquiry < ActiveRecord::Base
belongs_to :company
has_many :inquiry_items
accepts_nested_attributes_for :inquiry_items, allow_destroy: true, reject_if: lambda { |a| a[:item_id].blank? && a[:quantity].blank? }
end
查询项目
class InquiryItem < ActiveRecord::Base
belongs_to :inquiry
belongs_to :item
has_many :inquiry_item_prices
accepts_nested_attributes_for :inquiry_item_prices, allow_destroy: true, reject_if: lambda { |a| a[:item_id].blank? && a[:quantity].blank? }
end
InquiryItemPrices
class InquiryItemPrices < ActiveRecord::Base
belongs_to :inquiry_items
end
InquiriesController
def show
@request = Inquiry.find(params[:id])
@request.inquiry_items.each { |item| item.inquiry_item_prices.build}
end
show.html.slim
-@inquiry.inquiry_items.each do |inquiry_item|
tr
td =inquiry_item.item.name
td =inquiry_item.quantity
=f.fields_for :inquiry_item_prices do |builder|
td =builder.text_field :price, class:"form-control", placeholder:"Preço"
使用此代码,我为 price
加载了一个输入,如下所示:
<input class="form-control" placeholder="Preço" type="text" name="inquiry[inquiry_item_prices][price]" id="inquiry_inquiry_item_prices_price">
问题是它不是一个列表,所以我只会得到最后一个字段值,它应该是这样的:
<input class="form-control" placeholder="Preço" type="text" name="inquiry[inquiry_item_prices][0][price]" id="inquiry_inquiry_item_prices_0_price">
如何正确加载 InquiryItemPrice
并生成那些输入字段?
解决方案只是更新我的 show.html.slim
form
标记文件
=form_for(@inquiry, url: inquiry_add_prices_path(@inquiry)) do |f|
table class="table table-striped"
thead
tr
th Nome
th Quantidade
th Valor
tbody
=f.fields_for :inquiry_items do |item_form|
tr
td =item_form.object.item.name
td =item_form.object.quantity
=item_form.fields_for :inquiry_item_prices do |price_form|
td =price_form.text_field :price, class:"form-control", placeholder:"Preço"
=f.submit "Enviar preços", class: "btn btn-success"
这样就不需要循环 @inquiry.inquiry_items 因为它们是在控制器 show
方法中构建的。
我有一个 Inquiry
,它有很多 InquiryItem
模型,它有很多 InquiryItemPrice
。
在我的显示视图中,我正在遍历我的 inquiry_items
,对于其中的每一个,我都需要 inquiry_item_price
.
这些是我的模型:
询价
class Inquiry < ActiveRecord::Base
belongs_to :company
has_many :inquiry_items
accepts_nested_attributes_for :inquiry_items, allow_destroy: true, reject_if: lambda { |a| a[:item_id].blank? && a[:quantity].blank? }
end
查询项目
class InquiryItem < ActiveRecord::Base
belongs_to :inquiry
belongs_to :item
has_many :inquiry_item_prices
accepts_nested_attributes_for :inquiry_item_prices, allow_destroy: true, reject_if: lambda { |a| a[:item_id].blank? && a[:quantity].blank? }
end
InquiryItemPrices
class InquiryItemPrices < ActiveRecord::Base
belongs_to :inquiry_items
end
InquiriesController
def show
@request = Inquiry.find(params[:id])
@request.inquiry_items.each { |item| item.inquiry_item_prices.build}
end
show.html.slim
-@inquiry.inquiry_items.each do |inquiry_item|
tr
td =inquiry_item.item.name
td =inquiry_item.quantity
=f.fields_for :inquiry_item_prices do |builder|
td =builder.text_field :price, class:"form-control", placeholder:"Preço"
使用此代码,我为 price
加载了一个输入,如下所示:
<input class="form-control" placeholder="Preço" type="text" name="inquiry[inquiry_item_prices][price]" id="inquiry_inquiry_item_prices_price">
问题是它不是一个列表,所以我只会得到最后一个字段值,它应该是这样的:
<input class="form-control" placeholder="Preço" type="text" name="inquiry[inquiry_item_prices][0][price]" id="inquiry_inquiry_item_prices_0_price">
如何正确加载 InquiryItemPrice
并生成那些输入字段?
解决方案只是更新我的 show.html.slim
form
标记文件
=form_for(@inquiry, url: inquiry_add_prices_path(@inquiry)) do |f|
table class="table table-striped"
thead
tr
th Nome
th Quantidade
th Valor
tbody
=f.fields_for :inquiry_items do |item_form|
tr
td =item_form.object.item.name
td =item_form.object.quantity
=item_form.fields_for :inquiry_item_prices do |price_form|
td =price_form.text_field :price, class:"form-control", placeholder:"Preço"
=f.submit "Enviar preços", class: "btn btn-success"
这样就不需要循环 @inquiry.inquiry_items 因为它们是在控制器 show
方法中构建的。