gpt4 book ai didi

mysql - 动态生成的字段不保存

转载 作者:行者123 更新时间:2023-11-29 19:23:58 25 4
gpt4 key购买 nike

我有一个包含多个表的数据库。其中一个用作要输入到另一表中的静态信息源。我创建了一个表单,其中第一个表被循环并用于构建输入字段。这些字段有一个复选框,理想情况下,如果选中该复选框,则提交后会将该字段保存到第二个表中。

但是,它根本不保存到第二个表,而其余的表单(嵌套表单)可以。

这是怎么来的?我在服务器日志中没有收到任何错误。

_form 的一部分。总共包含 6 个这样的 block 和 2 个其他 block 。其他 block 确实保存正确:

 <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

最佳答案

如果我正确理解你的模型关系,在我看来,Inquiry 是应该具有用于​​测量的嵌套属性的模型,你应该

 pass accepts_nested_attributes_for :measurements

从调查测量到调查

关于mysql - 动态生成的字段不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42268159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com