gpt4 book ai didi

javascript - Cocoon 项目字段未将值发布到数据库

转载 作者:行者123 更新时间:2023-11-28 03:16:36 27 4
gpt4 key购买 nike

我有一个使用 Cocoon gem 生成的 lineItem 字段,在尝试保存行项目字段值时遇到问题,并且当我检查元素时,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

这是我的表单部分 View 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>

这是我的嵌套表单 View

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

这些是发送到 Controller 的值

"authenticity_token"=>"qMr1sDqfOVTdjVvXzC3Hvejh3KXnoyburvHXwu7L6Qg1mSDqnagLo0gSXrUcpJumMeb1u9pPLz9OyaYndbeqBA==","发票"=>{"日期"=>"2019/12/06","货币"=>"奈拉", “line_items_attributes”=>{“0 "=>{"描述"=>"大米", "数量"=>"2", "unit_cost"=>"56", "net_amount"=>"112.00", "_destroy"=>"false"}} }, "提交"=>"创建发票"} 不允许的参数::_destroy

最佳答案

从发布到 Controller 的参数中,我们可以看到它只有 1 个 line_item,并且没有动态添加的。

出现这种情况有几个可能的原因:

  • 使用 html-ids 来重复元素。例如,嵌套项目位于具有固定 html-id 的封装元素内:由于 html 中不允许重复 id,但可以正确呈现,因此在收集表单数据时会忽略它们。仅保存第一个元素。
  • 另一个常见原因是 link-to-add-association 在页面上插入嵌套元素,一切“看起来都不错”,但嵌套项目位于表单外部,因此在发布时从未收集表格。在这种情况下,只有新添加的项目将永远不会被保存。

关于javascript - Cocoon 项目字段未将值发布到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59590890/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com