Ruby on rails Rails在编辑嵌套表单时创建重复项

Ruby on rails Rails在编辑嵌套表单时创建重复项,ruby-on-rails,nested-forms,Ruby On Rails,Nested Forms,我在谷歌上搜索了很多次,然后又在允许的属性中添加了:id,但这在我的情况下不起作用,所以: 编辑嵌套表单时,如何防止创建多个空行(在相关表中)? 每次我添加表单时,它都会使空相关记录的数量加倍 控制员: class EnquiriesController < ApplicationController before_action :authenticate_user! load_and_authorize_resource # before_action :set_en

我在谷歌上搜索了很多次,然后又在允许的属性中添加了:id,但这在我的情况下不起作用,所以: 编辑嵌套表单时,如何防止创建多个空行(在相关表中)? 每次我添加表单时,它都会使空相关记录的数量加倍

控制员:

    class EnquiriesController < ApplicationController
  before_action :authenticate_user!
  load_and_authorize_resource
  # before_action :set_enquiry, only: [:show, :edit, :update, :destroy]
  #11-1-2017 Devise geinstalleerd, nog verder afmaken!(oa onderstaande uncommenten)


  # GET /enquiries
  # GET /enquiries.json
  def index
   # @enquiries = Enquiry.all
    @enquirie = current_user.enquiries
  end

  # GET /enquiries/1
  # GET /enquiries/1.json
  def show
  end

  # GET /enquiries/new
  def new
    @enquiry = Enquiry.new
    @enquiry.enquirymeasures.build
       #@enquiry.measurements.build
   @enquiry.tools.build
    @enquiry.build_applicant
    @enquiry.signatures.build
    @enquiry.gasmeters.build
       #@enquiry.enquirymeasures.build.build_measurement
  end

  # GET /enquiries/1/edit
  def edit
    #nog op kunnen slaan!!!! 1-01-17 Marco
    @enquiry = Enquiry.find(params[:id])
    @enquiry.enquirymeasures.build

    @enquiry.tools.build
    @enquiry.build_applicant
    @enquiry.signatures.build
   @enquiry.gasmeters.build

  end

  # POST /enquiries
  # POST /enquiries.json
  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

  #approved_enquiry_notification

  # PATCH/PUT /enquiries/1
  # PATCH/PUT /enquiries/1.json
  def update
    @enquiry = Enquiry.find(params[:id])
     respond_to do |format|
      if @enquiry.update(enquiry_params)
        format.html { redirect_to @enquiry, notice: 'Enquiry was successfully updated.' }
        format.json { render :show, status: :ok, location: @enquiry }
      else
        format.html { render :edit }
        format.json { render json: @enquiry.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /enquiries/1
  # DELETE /enquiries/1.json
  def destroy
    @enquiry.destroy
    respond_to do |format|
      format.html { redirect_to enquiries_url, notice: 'Enquiry was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

   private
    # Use callbacks to share common setup or constraints between actions.
    def set_enquiry
      @enquiry = Enquiry.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
  #25-1 MG alle attributes hadden ook :id
    def enquiry_params
      params.require(:enquiry).permit(:id, :reference, :location, :description, :date, :amount, :approved, enquirymeasures_attributes: [:id, :responsible, :done, :needed, :measurement_id, :user],
                                      tools_attributes: [:id, :handtool, :other, :motorvehicle, :compressor, :ramp, :scaffold, :crane, :ladder, :generator, :tankladder],
                                        applicant_attributes: [:id, :name, :email, :contractor_id],
                                      signatures_attributes: [:id, :date, :signature, :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]).merge(user_id: current_user.id)


    end



end
类查询控制器
模型: 主要内容:

课堂查询lambda{a | a[:responsible].blank?},:allow_destroy=>true的_嵌套_属性
有很多:工具,依赖::销毁
接受工具的\u嵌套\u属性\u
有很多:控件,依赖::销毁
接受控件的\u嵌套\u属性\u
有一个:申请人,家属::销毁
接受申请人的\u嵌套\u属性\u
有一个:承包商,通过::申请人
有很多:签名,依赖::销毁
接受签名的\u嵌套\u属性\u
有许多:代表,通过::签名,:source=>:代表
有很多:煤气表、煤气表、煤气表
接受燃气表的\u嵌套\u属性\u
属于:用户
#Goedkuring邮件
创建后:发送确认邮件
def发送确认邮件
ModelMailer.new\u查询\u邮件(self.deliver)
结束
#更新后:发送\u批准的\u邮件
#def发送批准的邮件
#如果参数[:已批准]=“1”
#如果:已批准==“1”
#模型邮递员。查询\批准\邮递员(自行)。递送
#结束
#结束
结束
相关模型之一:

class Tool < ActiveRecord::Base
  belongs_to :enquiry
end
class工具
我没有包括整个表格,因为它很长(300行),但我想告诉你:

 <div class="form-group ">

        <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">

          <%= f.fields_for :applicant do |a| %>

              <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                <div class="panel panel-default">
                  <div class="panel-heading" role="tab" id="headingOne">
                    <h4 class="panel-title">
                      <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                        Aanvraag
                      </a>
                    </h4>
                  </div>
                  <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
                    <div class="panel-body">
                      <%= f.label :Referentie, :class => "col-sm-2 control-label" %>
                      <%= f.text_field :reference, :class => "form-control" %> <br>

                      <%= f.label :Locatie, :class => "col-sm-2 control-label" %>
                      <%= f.select :location, [['Chemiepark', 'chemiepark'], ['Winschoten', 'winschoten'], ['Farmsum', 'farmsum']], :class => "form-control" %>
                      <br>
                      <br>

                      <%= f.label :Omschrijving, :class => "col-sm-2 control-label" %>
                      <%= f.text_area :description, :class => "form-control" %> <br>

                      <%= f.label :Aantal_uitvoerenden, :class => "col-sm-2 control-label" %>
                      <%= f.number_field :amount, :class => "form-control" %> <br>

                      <%= f.label :Datum %><br>
                      <%= f.datetime_select :date %> <br>

                      <%= a.label :Aanvrager %><br>
                      <%= a.text_field :name %> <br>

                      <%= a.label :email %><br>
                      <%= a.text_field :email %> <br>

                      <%= a.label :Aannemer %><br>
                      <%= a.collection_select(:contractor_id, Contractor.all, :id, :name, prompt: true) %>
                    </div>
                  </div>
                </div>
              </div>

          <% end %>

“col-sm-2控制标签”%>
“表单控件”%>
“col-sm-2控制标签”%> “表单控件”%>

“col-sm-2控制标签”%> “表单控件”%><
 <div class="form-group ">

        <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">

          <%= f.fields_for :applicant do |a| %>

              <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                <div class="panel panel-default">
                  <div class="panel-heading" role="tab" id="headingOne">
                    <h4 class="panel-title">
                      <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                        Aanvraag
                      </a>
                    </h4>
                  </div>
                  <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
                    <div class="panel-body">
                      <%= f.label :Referentie, :class => "col-sm-2 control-label" %>
                      <%= f.text_field :reference, :class => "form-control" %> <br>

                      <%= f.label :Locatie, :class => "col-sm-2 control-label" %>
                      <%= f.select :location, [['Chemiepark', 'chemiepark'], ['Winschoten', 'winschoten'], ['Farmsum', 'farmsum']], :class => "form-control" %>
                      <br>
                      <br>

                      <%= f.label :Omschrijving, :class => "col-sm-2 control-label" %>
                      <%= f.text_area :description, :class => "form-control" %> <br>

                      <%= f.label :Aantal_uitvoerenden, :class => "col-sm-2 control-label" %>
                      <%= f.number_field :amount, :class => "form-control" %> <br>

                      <%= f.label :Datum %><br>
                      <%= f.datetime_select :date %> <br>

                      <%= a.label :Aanvrager %><br>
                      <%= a.text_field :name %> <br>

                      <%= a.label :email %><br>
                      <%= a.text_field :email %> <br>

                      <%= a.label :Aannemer %><br>
                      <%= a.collection_select(:contractor_id, Contractor.all, :id, :name, prompt: true) %>
                    </div>
                  </div>
                </div>
              </div>

          <% end %>
def update
  @enquiry = Enquiry.find(params[:id])
  if @enquiry.update_attributes(enquiry_params)
    # Handle a successful update.
  else
    render 'edit'
end
has_many :tools, dependent: :destroy
accepts_nested_attributes_for :tools, reject_if: :all_blank

has_many :controls, dependent: :destroy
accepts_nested_attributes_for :controls, reject_if: :all_blank

has_one :applicant, dependent: :destroy
accepts_nested_attributes_for :applicant, reject_if: :all_blank

has_one :contractor, through: :applicant

has_many :signatures, dependent: :destroy
accepts_nested_attributes_for :signatures, reject_if: :all_blank

has_many :representatives , through: :signatures, :source => :representatives

has_many :gasmeters, dependent: :destroy
accepts_nested_attributes_for :gasmeters, reject_if: :all_blank