无法在rails中创建新记录

时间:2016-05-26 17:54:32

标签: mysql ruby ruby-on-rails-3

我尝试通过将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

在此您可以看到我有两个模型: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)

但服务器响应此错误:

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

2 个答案:

答案 0 :(得分:1)

我认为StockOrderStockOrderItems之间的关系是一对多的关系。

请参阅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
    }
 }

答案 1 :(得分:1)

假设存在has_many关联,则您的参数应为:

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