我有一个包含多个表的数据库。一个用作要在另一个表中输入的静态信息源。我创建了一个表单,其中第一个表被循环并用于构建输入字段。这些字段有一个复选框,理想情况下,如果选中该复选框,它会在提交后将该字段保存到第二个表中。
然而,它根本没有保存到第二个表,而其余部分则没有 形式(嵌套形式)确实。
这是怎么来的?我在服务器日志中没有收到任何错误。
_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
答案 0 :(得分:0)
如果我正确理解你的模型关系,在我看来,查询是应该具有测量的嵌套属性的模型,你应该
pass accepts_nested_attributes_for :measurements
从EnquiryMeasure到查询
答案 1 :(得分:0)
我找到了解决方案。 我正在使用:在enquirymeasure表中输入一个列名。由于这是一个保留者的话,所以这种情况并不起作用。 (我一直都知道的东西,但只是没有考虑或其他事情)