茧项目字段未将值发布到数据库中

问题描述 投票:0回答:1

我有一个使用Cocoon gem的通用行项目字段,尝试保存行项目字段值时遇到问题,当我检查元素时,add_item按钮没有为每个生成的新字段触发唯一ID。

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


def index
  @invoices = Invoice.all
end


def show

end

def new
   @invoice = Invoice.new
   @invoice.line_items.build
end


def edit

end


 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


 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

 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

 def set_line_item
  @line_item = LineItem.find(params[:id])
 end

# Never trust parameters from the scary internet, only allow the white list through.
#def invoice_params
  #params.require(:invoice).permit(:total_amount,  :issue_date, :currency, :description)
#end

def invoice_params
  params.require(:invoice).permit(:amount, :date, :currency, {line_items_attributes: [:id,:quantity,:net_amount, :description, :unit_cost]})
  end
 end

这是我的表单局部视图enter image description here

<div class="container">
    <div class="row">
            <div class="col">
              <%= f.label :date %>
               <%= f.input_field :date , class: "form-control datepicker", as: :string, id: "invoice_date" %>
             </div>
            <div class="col">
                <%= f.label :currency %>
                 <%= f.input_field :currency , class: "form-control" , id: "invoice_currency" %>
            </div>

            <div class="col">
                <h5>Amount Due : <span class="bal" id="invoice_total">679.00</span><span class="subtotal_currency">
                </span></h5>
            </div>
       </div>
     </div>
 </div>

    <table class="table">
          <thead>
              <tr>
                <th class="description">Description</th>
                <th class="quantity">Quantity</th>
                <th class="cost">Unit Cost</th>
                <th class="price">Total Amount</th>
                <th class="action">Action</th>
              </tr>
           </thead>
            <tbody class='line_items'>
              <div id="task">
                  <%= f.simple_fields_for :line_items do |items| %>
                  <%= render 'line_item_fields', f: items %>
                <% end %>   
           </tbody>
    </table>
                  <div class='links'>
                    <%= link_to_add_association 'add item', f, :line_items, class: "btn btn-primary",data: {"association-insertion-node" => "tbody.line_items", "association-insertion-method" => "append"} %>
                    <%= f.submit  class:"btn btn-primary" %>
                  </div>
              </div>
              <br>

这是我的嵌套表单视图

<tr class="nested-fields">
  <div class="container">
  <div class="row row-cols-5">
    <div class="col"> <td><%= f.text_field :description, class: "form-control item_desc" %></td></div><br/>
    <div class="col"> <td><%= f.text_field :quantity,  class: "form-control quantity" %></td></div><br/>
    <div class="col"> <td><%= f.text_field :unit_cost, class: "form-control unit_cost"%></td></div><br/>
    <div class="col"> <td class="price_td"><%= f.text_field  :net_amount, class: "form-control price", :readonly => true %></span> <span class= "subtotal_currency"></span></td></div><br/>
    <div class="col"> <td><%= link_to_remove_association 'Delete', f, class: 'remove_record btn btn-danger' %></td></div>
  </div>
</div>
</tr>

这是我的JavaScript文件

function update_price(){ 
    var row = $(this).parents('.nested-fields');
    var price = row.find('.unit_cost').val() * row.find('.quantity').val();
    price = price.toFixed(2);
    isNaN(price) ? row.find('.price').val('is not a number') : 
    row.find('.price').val(price);
    update_subtotal();    
}

$(document).on('cocoon:after-insert', '#task', function(added_task){
   // $('.links')
      //('cocoon:after-insert', function(e, added_task) {
       // console.log(added_task);
     // })
      $("#task").on('click', function(){
     console.log('looks like clicking works...'); //this works fine
   });

});

$(document).on('blur', '.unit_cost, .quantity', update_price)

这些是发布到控制器的值

[“ authenticity_token” =>“ qMr1sDqfOVTdjVvXzC3Hvejh3KXnoyburvHXwu7L6Qg1mSDqnagLo0gSXrUcpJumMeb1u9pPLz9OyaYndbeqBA ==”,“发票” =>“”“”>“”“”“ => {“描述” =>“大米”,“数量” =>“ 2”,“单位成本” =>“ 56”,“净额” =>“ 112.00”,“ _ destroy” =>“假”}}} ,“ commit” =>“创建发票”}不允许的参数::_destroy

javascript jquery ruby-on-rails nested-forms apache-cocoon
1个回答
0
投票

从发布到控制器的参数中,我们只能看到它是1 line_item,没有一个是动态添加的。

© www.soinside.com 2019 - 2024. All rights reserved.