如何将API参数映射到模型

时间:2016-02-02 14:08:38

标签: ruby-on-rails ruby api curl shopify

我是Ruby和Ruby on Rails的新手,我正努力让我的API工作。我正在尝试创建一个接受Shopify的POST请求的API(在我的应用程序中创建订单)。我知道进来的消息的结构(我在curl命令中使用了一个示例消息)并且它们与我的模型的列不匹配,因此我需要映射它们。我发现很难找到有关如何执行此操作的任何文档。

每当我尝试使用下面的-curl命令创建订单时,我都会收到此错误:

    ArgumentError (wrong number of arguments (15 for 0..2)):
  app/controllers/api/v1/orders_controller.rb:16:in `create'

我的卷曲命令:

curl -i -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{
       "id":2429839748,
       "email":"jlaremore@woh.rr.com",
       "closed_at":null,
       "created_at":"2016-01-28T07:55:01-07:00",
       "updated_at":"2016-01-28T07:55:02-07:00",
       "number":97539,
       "note":null,
       "token":"b03b758395d043e9a883bd5bededed8d",
       "gateway":"paypal",
       "test":false,
       "total_price":"126.00",
       "subtotal_price":"126.00",
       "total_weight":7189,
       "total_tax":"0.00",
       "taxes_included":false,
       "currency":"USD",
       "financial_status":"paid",
       "confirmed":true,
       "total_discounts":"0.00",
       "total_line_items_price":"126.00",
       "cart_token":"bb3560e80e353582d93e4e6aaa006757",
       "buyer_accepts_marketing":true,
       "name":"#98539",
       "referring_site":"https:\/\/www.earthled.com\/collections\/dimmable-led-light-bulbs\/products\/thinklux-led-par38-17-5-watts-85-watt-equal-dimmable-outdoor-wet-rated-tkupar38?variant=3554253636",
       "landing_site":"\/cart",
       "cancelled_at":null,
       "cancel_reason":null,
       "total_price_usd":"126.00",
       "checkout_token":"898a8d893fe2f10647c8ccd8941515d1",
       "reference":null,
       "user_id":null,
       "location_id":null,
       "source_identifier":null,
       "source_url":null,
       "processed_at":"2016-01-28T07:55:01-07:00",
       "device_id":null,
       "browser_ip":"98.31.59.11",
       "landing_site_ref":null,
       "order_number":98539,
       "tags":"",
       "contact_email":"jlaremore@woh.rr.com",
       "line_items":[
          {
             "id":4412721796,
             "variant_id":3554253636,
             "title":"Thinklux LED PAR38 - 17.5 Watts - 85 Watt Equal - Dimmable - Outdoor\/Wet Rated",
             "quantity":8,
             "price":"15.75",
             "grams":900,
             "sku":"TKUPAR38-17.5W-830-25D",
             "variant_title":"3000K \/ 1 Piece",
             "vendor":"Thinklux",
             "fulfillment_service":"manual-wg",
             "product_id":905965252,
             "requires_shipping":true,
             "taxable":true,
             "gift_card":false,
             "name":"Thinklux LED PAR38 - 17.5 Watts - 85 Watt Equal - Dimmable - Outdoor\/Wet Rated - 3000K \/ 1 Piece",
             "variant_inventory_management":"shopify",
             "properties":[

             ],
             "product_exists":true,
             "fulfillable_quantity":0,
             "total_discount":"0.00",
             "fulfillment_status":"fulfilled",
             "tax_lines":[

             ],
             "origin_location":{
                "id":559988100,
                "country_code":"US",
                "province_code":"CO",
                "name":"EarthLED.com",
                "address1":"601 16th Street",
                "address2":"Suite C-279",
                "city":"Golden",
                "zip":"80401"
             },
             "destination_location":{
                "id":913894148,
                "country_code":"US",
                "province_code":"OH",
                "name":"Susan Laremore",
                "address1":"4283 County Rd 175",
                "address2":"",
                "city":"Clyde",
                "zip":"43410"
             }
          }
       ],
       "shipping_lines":[
          {
             "id":2175393924,
             "title":"FREE Ground Shipping",
             "price":"0.00",
             "code":"FREE Ground Shipping",
             "source":"shopify",
             "phone":null,
             "tax_lines":[

             ]
          }
       ],
       "shipping_address":{
          "first_name":"Susan",
          "address1":"4283 County Rd 175",
          "phone":"4195478501",
          "city":"Clyde",
          "zip":"43410",
          "province":"Ohio",
          "country":"United States",
          "last_name":"Laremore",
          "address2":"",
          "company":"Class Act Dance Studio",
          "latitude":41.283498,
          "longitude":-82.966808,
          "name":"Susan Laremore",
          "country_code":"US",
          "province_code":"OH"
       },
       "fulfillments":[
          {
             "id":2130025412,
             "order_id":2429839748,
             "status":"success",
             "created_at":"2016-01-28T07:55:02-07:00",
             "service":"manual-wg",
             "updated_at":"2016-01-28T07:55:02-07:00",
             "tracking_company":null,
             "tracking_number":null,
             "tracking_numbers":[

             ],
             "tracking_url":null,
             "tracking_urls":[

             ],
             "receipt":{

             },
             "line_items":[
                {
                   "id":4412721796,
                   "variant_id":3554253636,
                   "title":"Thinklux LED PAR38 - 17.5 Watts - 85 Watt Equal - Dimmable - Outdoor\/Wet Rated",
                   "quantity":8,
                   "price":"15.75",
                   "grams":900,
                   "sku":"TKUPAR38-17.5W-830-25D",
                   "variant_title":"3000K \/ 1 Piece",
                   "vendor":"Thinklux",
                   "fulfillment_service":"manual-wg",
                   "product_id":905965252,
                   "requires_shipping":true,
                   "taxable":true,
                   "gift_card":false,
                   "name":"Thinklux LED PAR38 - 17.5 Watts - 85 Watt Equal - Dimmable - Outdoor\/Wet Rated - 3000K \/ 1 Piece",
                   "variant_inventory_management":"shopify",
                   "properties":[

                   ],
                   "product_exists":true,
                   "fulfillable_quantity":0,
                   "total_discount":"0.00",
                   "fulfillment_status":"fulfilled",
                   "tax_lines":[

                   ],
                   "origin_location":{
                      "id":559988100,
                      "country_code":"US",
                      "province_code":"CO",
                      "name":"EarthLED.com",
                      "address1":"601 16th Street",
                      "address2":"Suite C-279",
                      "city":"Golden",
                      "zip":"80401"
                   },
                   "destination_location":{
                      "id":913894148,
                      "country_code":"US",
                      "province_code":"OH",
                      "name":"Susan Laremore",
                      "address1":"4283 County Rd 175",
                      "address2":"",
                      "city":"Clyde",
                      "zip":"43410"
                   }
                }
             ]
          }
       ],
       "customer":{
          "id":2556660676,
          "email":"jlaremore@woh.rr.com",
          "accepts_marketing":true,
          "created_at":"2016-01-28T07:51:34-07:00",
          "updated_at":"2016-01-28T07:55:01-07:00",
          "first_name":"Susan",
          "last_name":"Laremore",
          "orders_count":1,
          "state":"disabled",
          "total_spent":"126.00",
          "last_order_id":2429839748,
          "note":null,
          "verified_email":true,
          "multipass_identifier":null,
          "tax_exempt":false,
          "tags":"",
          "last_order_name":"#98539",
          "default_address":{
             "id":2697648836,
             "first_name":"Susan",
             "last_name":"Laremore",
             "company":"Class Act Dance Studio",
             "address1":"4283 County Rd 175",
             "address2":"",
             "city":"Clyde",
             "province":"Ohio",
             "country":"United States",
             "zip":"43410",
             "phone":"4195478501",
             "name":"Susan Laremore",
             "province_code":"OH",
             "country_code":"US",
             "country_name":"United States",
             "default":true
          }
       }
    }' http://localhost:3000/api/v1/orders

我在api / v1 / orders_controller.rb中创建函数

    def create
        order = Order.new(
            id = params[:id],
            shopify_order_id = params[:id],
            shopify_created_at = params[:created_at],
            shopify_order_name = params[:name],
            ship_to_name = params[:shipping_address][:name],
            ship_to_address_1 = params[:shipping_address][:address1],
            ship_to_address_2 = params[:shipping_address][:address2],
            ship_to_company = params[:shipping_address][:company],
            ship_to_city = params[:shipping_address][:city],
            ship_to_state = params[:shipping_address][:state],
            ship_to_country = params[:shipping_address][:country],
            ship_to_zipcode = params[:shipping_address][:zip],
            ship_to_phonenumber = params[:shipping_address][:phone],
            ship_to_email = params[:shipping_address][:email],
            store_id = 1
        )
        if order.save?
            head 200
        else
            head 422
        end
    end

谁能告诉我我做错了什么?

2 个答案:

答案 0 :(得分:1)

你不应该使用= but:

像这样:

 order = Order.new(
        shopify_order_id: params[:id],
        ... etc
    )

请注意,我也省略了id属性,它会在您保存记录时自动设置。

同时更改

if order.save?
  head 200
else
  head 422
end

要:

if order.save
  head 200
else
  head 422
end

答案 1 :(得分:0)

  1. Order.new替换为Order.create
  2. 删除' id'来自创建哈希列表
  3. 并将分配列表替换为哈希。

    所以

    order = Order.create(
        shopify_order_id: params[:id],
        shopify_created_at: params[:created_at],
        shopify_order_name: params[:name],
        ship_to_name: params[:shipping_address][:name],
        ship_to_address_1: params[:shipping_address][:address1],
        ship_to_address_2: params[:shipping_address][:address2],
        ship_to_company: params[:shipping_address][:company],
        ship_to_city: params[:shipping_address][:city],
        ship_to_state: params[:shipping_address][:state],
        ship_to_country: params[:shipping_address][:country],
        ship_to_zipcode: params[:shipping_address][:zip],
        ship_to_phonenumber: params[:shipping_address][:phone],
        ship_to_email: params[:shipping_address][:email],
        store_id: 1)
    
  4. 附加说明

    最好将地址数据分配给名为Address的模型,然后因为您必须创建订单(使用CRUD的POST操作),任何id和{ {1}}字段将被省略,因此您将获得类似的内容:

    *at