ruby on rails - Edit action with nested form always adds new form fields under form records -


i cant figure out happening nested form. when create new invoice form works fine, when go invoice edit action see saved records , new empty nested form underneath. how stop happening?

my form

<div class="col-xs-12"> <%= simple_form_for(@invoice) |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 |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 item', '', :class => 'duplicate_nested_form' %>     <%= f.button :submit, 'submit payment', class: 'btn btn-warning btn-sm', id: "submit_invoice" %>       </div> </div> 

my invoices controller

class invoicescontroller < applicationcontroller before_action :set_invoice, only: [:show, :edit, :update, :destroy]  # /invoices # /invoices.json def index @invoices = invoice.all end  # /invoices/1 # /invoices/1.json def show @invoice = invoice.find(params[:id]) @items = @invoice.items end  # /invoices/new def new @invoice = invoice.new @invoice.items.build # end  # /invoices/1/edit def edit @invoice.items.build # end  # post /invoices # post /invoices.json def create @invoice = invoice.new(invoice_params)   respond_to |format|   if @invoice.save     format.html { redirect_to @invoice, notice: 'invoice 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 |format|   if @invoice.update(invoice_params)     format.html { redirect_to @invoice, notice: 'invoice 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 deleted." respond_to |format|   format.html { redirect_to invoices_url, notice: 'invoice destroyed.' }   format.json { head :no_content } end end  private # use callbacks share common setup or constraints between actions. def set_invoice   @invoice = invoice.find(params[:id]) end  # never trust parameters scary internet, allow 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 

and here invoices/edit view

<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> 

this because have added @invoice.items.build in edit action. creates new item instance @invoice.

try this

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

i hope helpfull.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -