使用嵌套表单编辑操作始终在表单记录下添加新表单字段

时间:2016-01-21 05:07:30

标签: ruby-on-rails nested-attributes

我无法弄清楚这个嵌套表单发生了什么。当我创建新发票时,表单工作正常,但当我转到发票编辑操作时,我会看到保存的记录和下面的新空嵌套表单。 我该如何阻止这种情况发生?

我的表格

<div class="col-xs-12">
<%= simple_form_for(@invoice) do |f| %>
<%= f.error_notification %>
<div class="row">

<div class="row invoice-info">
  <div class="col-xs-4 invoice-col">

      <%= f.input_field :company, class: "form-control", id: "1" %>
      <%= f.input_field :contragent, class: "form-control", id: "5" %>
      <%= (Invoice.last.present? ? (Invoice.last.id + 1) : 1) %>
      <%= f.label :date, required: false %>
      <%= f.input_field :date, class: "form-control datepicker", as: :string, id: "invoice_date" %>
      <%= f.label :currency, required: false %>
      <%= f.input_field :currency, id:"invoice_currency", class: "form-control" %>

      <%= f.simple_fields_for :items do |h| %>
      <div class="duplicatable_nested_form">
      <p>Name</p>
      <%= h.input_field :item_name, class: "form-control" %>
      <p>Description</p>
      <%= h.input_field :item_description, class: "form-control" %>
      <p>Cost</p>
      <%= h.input_field :item_cost, class: "form-control cost", id: "price" %>
      <p>Quantity</p>
      <%= h.input_field :item_quantity, class: "form-control qty", id: "quantity" %>

      <td>Delete: <%= h.check_box :_destroy %></td>
      <% if h.object.new_record? %>
      <%= link_to 'Remove', '', :remote => true, :class => 'destroy_duplicate_nested_form' %>
      <% else %>
      <%= link_to 'Remove', invoice_item_path(@invoice, h.object), :method => :delete, :remote => true, :class => 'destroy_duplicate_nested_form' %>
      <%= h.input :id, as: :hidden %>
      <% end %>
      <% end %>
      </div>

      <%=f.hidden_field :amount, id: "invoice_total1" %>

<div class="row">
  <div class="col-xs-12 pull-left">
    <a class="btn btn-info btn-sm" id="invoice_button" data-toggle="modal" data-target="#invoice_modal">Show Invoice</a>

    <%= link_to 'Add Another Item', '', :class => 'duplicate_nested_form' %>
    <%= f.button :submit, 'Submit Payment', class: 'btn btn-warning btn-sm', id: "submit_invoice" %>    
  </div>
</div>

我的发票控制器

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
@invoice = Invoice.find(params[:id])
@items = @invoice.items
end

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

# GET /invoices/1/edit
def edit
@invoice.items.build #
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
flash[:success] = "Invoice was deleted."
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(:amount, :company, :contragent, :currency, :date, items_attributes: [ :item_name, :item_description, :item_cost, :item_quantity, :item_price, :id, :invoice_id, :_destroy ])
end
end

这是我的发票/编辑视图

<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">   
  <div class="panel-heading">        
    <h3 class="panel-title">Edit Invoice</h3>           
  </div>
        <div class="panel-body">
                <%= render 'form' %>
            </div>

            <div class="panel-footer">
                <%= link_to 'Back', invoices_path, class: "btn btn-danger" %>
                <%= link_to 'Show', @invoice, class: "btn btn-success" %>
            </div>

    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

这是因为您在@invoice.items.build操作中添加了edit。它为item创建了一个新的@invoice实例。

试试这个

def edit
  (1-@invoice.items.count).times { @invoice.items.build } #
end

我希望这会有所帮助。