Order from API

This commit is contained in:
redfast00 2019-05-12 23:09:18 +02:00
parent c484741c16
commit 0aab29f0cb
No known key found for this signature in database
GPG Key ID: 5946E0E34FD0553C
2 changed files with 28 additions and 6 deletions

View File

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

View File

@ -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)