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

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

怎么会这样服务器日志中没有任何错误。

_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

最佳答案

如果我正确理解了模型之间的关系,在我看来,查询是应该具有嵌套属性以进行度量的模型,您应该

 pass accepts_nested_attributes_for :measurements


从询价到询价

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

10-15 04:15
查看更多