我試圖通過將JSON對象從我的前端傳遞到服務器來創建新的StockOrder
。對於StockOrders
定義的參數強看起來是這樣的:無法在rails中創建新記錄
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!-------------------------
嘗試做'把stock_order_params.class'或'放stock_order_params。在@stock_order ='StockOrder.new(stock_order_params)'之前檢查',這會讓你清楚你正在處理的事情。 – imechemi
@imechemi完成 - 請參閱我的編輯 – Katana24
如果您嘗試打印'stock_order_params.class',則這是不可能的。它應該打印可能是JSON,Hash或String的數據類型。它不應該打印強參數的內容。 – imechemi