gpt4 book ai didi

mysql - 无法在rails中创建新记录

转载 作者:行者123 更新时间:2023-11-29 07:17:20 25 4
gpt4 key购买 nike

我尝试通过将 JSON 对象从前端传递到服务器来创建新的 StockOrderStockOrders 定义的强参数如下所示:

private
def stock_order_params
params.require(:stock_order).permit( [StockOrder.strong_params, :purchaser_id, :carriage_terms, :carriage_cost, :contact_id, :user_id, :currency, :default_vat_rate, :discount_cost, :dispatched_status, :due_date, :internal_notes, :invoice_address_id, :invoice_date, :payment_terms, :po_date, :vat_rate, :purchase_order_number, {stock_order_line_items_attributes: [StockOrderLineItem.strong_params, :_destroy, :id, :part_id, :description, :quantity, :unit_cost, :vat_rate, :quantity_to_dispatch, :sort_index] + StockOrderLineItem.additional_params}, :purchaser_notes, :delivery_address] + StockOrder.additional_params )
end

在此您可以看到我有两个模型:StockOrderStockOrderLineItem

这是我从前端发送的内容:

{
"stock_order":
{
"stock_order_line_items_attributes":
{
"part_id":2309,"unit_cost":15,
"quantity_to_dispatch":5
},
"contact_id":10,
"purchaser_id":10
}
}

最后,这是我尝试创建新的 StockOrder 的方法:

@stock_order = StockOrder.new(stock_order_params)

但是服务器响应此错误:

Rails controller error

但是我已经查看了这两个模型的模型,并且在我传递的参数中,没有一个是字符串。以下是表架构的快照:

# == Schema Information
#
# Table name: stock_orders
#
# id :integer not null, primary key
# ref_no :integer
# purchase_order_number :string
# contact_id :integer
# purchase_order_date :date
# carriage_terms :text
# payment_terms :text
# due_date :date
# purchaser_id :integer

对于StockOrderItemList

# == Schema Information
#
# Table name: stock_order_line_items
#
# id :integer not null, primary key
# stock_order_id :integer
# part_id :integer
# quantity :decimal(, )
# vat_rate :float
# unit_cost_cents :integer
# net_cost_cents :integer
# total_cost_cents :integer
# vat_cost_cents :integer
# local_unit_cost_cents :integer
# local_net_cost_cents :integer
# local_total_cost_cents :integer
# local_vat_cost_cents :integer
# quantity_dispatched :decimal(, )
# created_at :datetime not null
# updated_at :datetime not null
# description :text
#

编辑

这是 puts.stock_order_params.class 打印出来的内容

Parameters: {
"stock_order"=>{
"stock_order_line_items_attributes"=>{
"part_id"=>2309,
"unit_cost"=>15,
"quantity_to_dispatch"=>5
},
"contact_id"=>10,
"purchaser_id"=>10}
}

编辑2

19:34:59 web.1    | App 31944 stdout: ----------------------HERE!-------------------------
19:34:59 web.1 | App 31944 stdout: MagicField Load (0.7ms) SELECT "magic_fields".* FROM "magic_fields" WHERE "magic_fields"."model_owner" = $1 [["model_owner", "StockOrder"]]
19:34:59 web.1 | App 31944 stdout: MagicField Load (0.3ms) SELECT "magic_fields".* FROM "magic_fields" WHERE "magic_fields"."model_owner" = $1 [["model_owner", "StockOrderLineItem"]]
19:34:59 web.1 | App 31944 stdout: ActionController::Parameters
19:34:59 web.1 | App 31944 stdout: ----------------------THERE!-------------------------

最佳答案

我假设 StockOrderStockOrderItems 之间的关系是一对多关系。

参见this guide to nested attributes .

您应该向 Controller 发送一组 stock_order_line_items 哈希值,而不是单个哈希值(适用于 One-to-One relationships )。

也就是说:

{
"stock_order": {
"stock_order_line_items_attributes": [
{
"part_id":2309,
"unit_cost":15,
"quantity_to_dispatch":5
}
],
"contact_id":10,
"purchaser_id":10
}
}

关于mysql - 无法在rails中创建新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37467915/

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