我有一個包含多個表的數據庫。其中一個用作靜態信息源以輸入其他表格。我創建了一個表格,第一個表格被循環使用並用於構建輸入字段。這些字段有一個複選框,理想情況下,如果複選框被選中,它會在提交後將該字段保存到第二個表中。動態生成的字段沒有保存
但是,它根本不會保存到第二個表中,而其餘的 表單(嵌套表單)的確如此。
這是怎麼回事?我在服務器日誌中沒有任何錯誤。
_form的一部分。總共包含6個這樣的塊和2個其他塊。其他塊沒有正確保存:
<div class="card">
<div class="card-header" role="tab" id="headingTwo">
<h5 class="mb-0">
<a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Product (vrij) verklaring
</a>
</h5>
</div>
<div id="collapseTwo" class="collapse" role="tabpanel" aria-labelledby="headingTwo">
<div class="card-block">
<%= f.fields_for :enquirymeasures do |em| %>
<%= Measurement.where(measurementtype: 'product verklaring').find_each do |m| %>
<%= f.fields_for :enquirymeasures, :html => {:multipart => true} do |em| %>
<%# em.hidden_field :measurementid, {:value => m.id} %>
<%= em.text_field :type, {:value => m.measurementtype} %>
<%= em.text_field :description, {:value => m.measurement} %>
<p>
<%= em.check_box :needed %>
<%= m.measurement %>
<hr>
<% end %>
<% end %>
<%= f.fields_for :enquirymeasures do |em| %>
<%= em.label :bijzonderheden %>
<%= em.text_field :other %></p>
<% end %>
<br>
</div>
</div>
<% end %>
</div>
<%= f.fields_for :enquirymeasures do |em| %>
<div class="card">
<div class="card-header" role="tab" id="headingElektro">
<h5 class="mb-0">
<a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseElektro" aria-expanded="false" aria-controls="collapseElektro">
Elektrotechnische maatregelen
</a>
</h5>
</div>
<div id="collapseElektro" class="collapse" role="tabpanel" aria-labelledby="headingElektro">
<div class="card-block">
<%= Measurement.where(measurementtype: 'Elektrotechnisch').find_each do |m| %>
<%= f.fields_for :enquirymeasures, :html => {:multipart => true} do |em| %>
<%= em.hidden_field :measurementid, {:value => m.id} %>
<%= em.hidden_field :type, {:value => m.measurementtype} %>
<%= em.hidden_field :description, {:value => m.measurement} %>
<p><%= em.check_box :needed %> <%= m.measurement %>
<hr>
<% end %>
<% end %>
<%= f.fields_for :enquirymeasures do |em| %>
<%= em.label :bijzonderheden %>
<%= em.text_field :other %></p>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
允許的屬性:
def enquiry_params
params.require(:enquiry).permit(:reference, :location, :description, :date, :amount, :approved, :status,
enquirymeasures_attributes: [:id, :done, :needed, :measurement_id, :measurementid, :user, :other, :type, :description],
tools_attributes: [:id, :handtool, :other, :motorvehicle, :compressor, :ramp, :scaffold, :crane, :ladder, :generator, :tankladder],
applicant_attributes: [:id, :name, :email, :contractor_id, :company],
signatures_attributes: [:id, :date, :representative_id],
gasmeters_attributes: [:id, :date, :tester, :signature, :oxigen, :o_needed, :o_continu, :explosives, :e_needed, :e_continu, :mat1, :mat1_needed, :mat1_continu, :mat2, :mat2_needed, :mat2_continu, :mat3, :mat3_needed, :mat3_continu],
controls_attributes: [:id, :enquirycheck, :workspacecheck, :enquiry_id],
roles_attributes: [:name],
user_attributes: [:id, :name, :company]).merge(user_id: current_user.id)
end
的創建功能
def create
@enquiry.user_id = current_user.id
@enquiry = Enquiry.new(enquiry_params)
#@enquiry.enquirymeasures.build
respond_to do |format|
if @enquiry.save
format.html { redirect_to @enquiry, notice: 'Enquiry was successfully created.' }
format.json { render :show, status: :created, location: @enquiry }
else
format.html { render :new }
format.json { render json: @enquiry.errors, status: :unprocessable_entity }
end
end
end
的數據庫模型:
table #1class Measurement < ActiveRecord::Base
belongs_to :enquirymeasure
belongs_to :enquiry
def type_and_measurement
"#{self.measurementtype} |" " #{self.measurement}"
end
end
table #2
class Enquirymeasure < ActiveRecord::Base
belongs_to :enquiry
has_many :measurements
accepts_nested_attributes_for :measurements
end