我试图通过将JSON对象从前端传递到服务器来创建新的StockOrder
。 StockOrders
定义的强参数如下所示:
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
在此您可以看到我有两个模型:
StockOrder
和StockOrderLineItem
。这是我从前端发送的内容:
{
"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)
但是服务器响应此错误:
但是我已经查看了这两个模型,并且在我传递的参数中,它们都不是字符串。这是表的架构的快照:
# == 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!-------------------------
最佳答案
我假设StockOrder
和StockOrderItems
之间的关系是一对多关系。
请参见this guide to nested attributes。
您应该向控制器发送一个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
}
}