From 0aab29f0cb9562a53da4afae28ba1e06e0c23ef5 Mon Sep 17 00:00:00 2001 From: redfast00 Date: Sun, 12 May 2019 23:09:18 +0200 Subject: [PATCH] Order from API --- app/controllers/orders_controller.rb | 20 ++++++++++++++------ python_api_example/order.py | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 python_api_example/order.py diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index cf64b3f..20b4a3a 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -22,12 +22,20 @@ class OrdersController < ApplicationController end def create - if @order.save - flash[:success] = @order.flash_success - redirect_to root_path - else - @products = Product.all.for_sale.order(:name) - render 'new' + respond_to do |format| + format.html do + if @order.save + flash[:success] = @order.flash_success + redirect_to root_path + else + @products = Product.all.for_sale.order(:name) + render 'new' + end + end + format.json do + @order.save + render json: @order + end end end diff --git a/python_api_example/order.py b/python_api_example/order.py new file mode 100644 index 0000000..1bb001e --- /dev/null +++ b/python_api_example/order.py @@ -0,0 +1,14 @@ +import requests + + +base_url = 'http://localhost:3000' +user = 'j' +user_token = 'uiUTrjuD3ZSft6s8JD9S4g==' + +headers = {'Authorization': f'Token token={user_token}'} +product = 1 + +data = {"order": {"order_items_attributes": [{"count": 1, "product_id": product}]}} + +r = requests.post(f'{base_url}/users/{user}/orders.json', headers=headers, json=data) +print(r.text)