Reputation: 8959
I'm attempting to create a new StockOrder
by passing a JSON object from my front end to the server. The defined strong parameters for the StockOrders
looks like this:
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
In this you can see that I have two models: StockOrder
& StockOrderLineItem
.
Here is what I'm sending from my front-end:
{
"stock_order":
{
"stock_order_line_items_attributes":
{
"part_id":2309,"unit_cost":15,
"quantity_to_dispatch":5
},
"contact_id":10,
"purchaser_id":10
}
}
And finally here is how I'm trying to create my new StockOrder
:
@stock_order = StockOrder.new(stock_order_params)
But the server responds with this error:
But I've looked at the models for both and, of the parameters that I'm passing, none of them are strings. Here's a snapshot of the schema for the tables:
# == 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
And for the 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
#
EDIT
This is what the puts.stock_order_params.class
prints out
Parameters: {
"stock_order"=>{
"stock_order_line_items_attributes"=>{
"part_id"=>2309,
"unit_cost"=>15,
"quantity_to_dispatch"=>5
},
"contact_id"=>10,
"purchaser_id"=>10}
}
EDIT 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!-------------------------
Upvotes: 0
Views: 48
Reputation: 1189
Assuming that there is an has_many
association, your parameters should be like:
Parameters: {
"stock_order"=>{
"stock_order_line_items_attributes"=>[
{
"part_id"=>2309,
"unit_cost"=>15,
"quantity_to_dispatch"=>5
}
],
"contact_id"=>10,
"purchaser_id"=>10
}
}
Upvotes: 1
Reputation: 81
I assume the relation between StockOrder
and StockOrderItems
is a One-to-many relationship.
See this guide to nested attributes.
You should send to your controller an array of stock_order_line_items
hashes instead of a single one hash (which is for One-to-One relationships).
That is to say:
{
"stock_order": {
"stock_order_line_items_attributes": [
{
"part_id":2309,
"unit_cost":15,
"quantity_to_dispatch":5
}
],
"contact_id":10,
"purchaser_id":10
}
}
Upvotes: 1