2016-08-24 41 views
1

我正在做一个发票应用程序,我有@invoice表单,里面我嵌套了客户,产品和公司信息的表单。产品表格在所有视图中工作正常,但客户表单不是。当我填写客户信息并创建新发票时,它就可以工作。但是,当我尝试编辑该发票时,整个表单都不见了。嵌套窗体工作在新的但没有显示在编辑轨道

发票/ _form

<%= form_for @invoice do |f| %> 
<% if @invoice.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@invoice.errors.count, "error") %> prohibited this invoice from being saved:</h2> 

     <ul> 
     <% @invoice.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <%= f.fields_for :customer do |customer| %> 
    <div class="field"> 
    <%= customer.label 'Bedrijfsnaam ontvanger' %><br/> 
    <%= customer.text_field :company_name, placeholder: 'bedrijfsnaam', class: 'form-control' %> 
    </div> 
    <div class="field"> 
    <%= customer.label 'Adres ontvanger' %><br> 
    <%= customer.text_field :address_line_1, placeholder: 'adres ontvanger', class: 'form-control' %> 
    </div> 
    <div class="field"> 
    <%= customer.label 'Postcode & stad' %><br> 
    <%= customer.text_field :zip_code, placeholder: '1234AB Rotterdam', class: 'form-control' %> 
    </div> 
    <% end %> 

Invoices_controller.rb

class InvoicesController < ApplicationController 
    before_action :set_invoice, only: [:show, :edit, :update, :destroy] 

    # GET /invoices 
    # GET /invoices.json 
    def index 
    @invoices = Invoice.all 
    end 

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

    # GET /invoices/new 
    def new 
    @invoice = Invoice.new 
    @invoice.products.build 
    @invoice.build_customer 
    end 

    # GET /invoices/1/edit 
    def edit 
    end 

    # POST /invoices 
    # POST /invoices.json 
    def create 
    @invoice = Invoice.new(invoice_params) 

    respond_to do |format| 
     if @invoice.save 
     format.html { redirect_to @invoice, notice: 'Invoice was successfully created.' } 
     format.json { render :show, status: :created, location: @invoice } 
     else 
     format.html { render :new } 
     format.json { render json: @invoice.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /invoices/1 
    # PATCH/PUT /invoices/1.json 
    def update 
    respond_to do |format| 
     if @invoice.update(invoice_params) 
     format.html { redirect_to @invoice, notice: 'Invoice was successfully updated.' } 
     format.json { render :show, status: :ok, location: @invoice } 
     else 
     format.html { render :edit } 
     format.json { render json: @invoice.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /invoices/1 
    # DELETE /invoices/1.json 
    def destroy 
    @invoice.destroy 
    respond_to do |format| 
     format.html { redirect_to invoices_url, notice: 'Invoice was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

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

    # Never trust parameters from the scary internet, only allow the white list through. 
    def invoice_params 
     params.require(:invoice).permit(:number, :currency, :date, :duedate, :btwtotal, 
             :subtotal, :total, :footer, customers_attributes: [:id, :company_name, :address_line_1, :zip_code, :_destroy], 
             companies_attributes: [:id, :btw_number, :iban_number, :kvk_number, :company_name, :_destroy], 
             products_attributes: [:id, :quantity, :description, :unitprice, :btw, :total]) 
    end 
end 

Invoice.rb - (模型)

class Invoice < ActiveRecord::Base 

    has_one :company 
    has_one :customer 
    has_many :products 

    accepts_nested_attributes_for :customer, reject_if: :all_blank, allow_destroy: true 
    accepts_nested_attributes_for :products, reject_if: :all_blank, allow_destroy: true 
    validates :number, :currency, :date, :duedate, :btwtotal, :subtotal, :total, presence: true 

end 
+0

你是什么新的和编辑视图的样子?您标记为“invoice_form”的内容看起来像一个部分,所以我们需要查看是什么导致部分内容。 – kcdragon

+0

新的和编辑的意见只是渲染'_form'部分 – luissimo

回答

1

在发票控制器尝试在强PARAMS改变customers_attributescustomer_attributes

customer_attributes: [:id, :company_name, :address_line_1, :zip_code, :_destroy] 

我怀疑这是你的客户嵌套的属性没有被正确保存的问题,所以当你去编辑观点的一部分表单没有被呈现,因为没有任何客户为您的发票保存

+0

哦哇,我完全忽略了那个..谢谢人现在工作! – luissimo

+0

不客气:) – Ren