tap/app/controllers/orders_controller.rb

44 lines
888 B
Ruby
Raw Normal View History

2014-11-06 18:56:00 +01:00
class OrdersController < ApplicationController
2014-11-25 15:27:12 +01:00
def overview
2014-12-09 13:10:36 +01:00
@users_by_name = User.all.order(:name)
2014-12-09 14:45:18 +01:00
@users_by_order = User.all
2014-11-06 18:56:00 +01:00
end
2014-11-10 02:30:42 +01:00
2014-11-25 15:27:12 +01:00
def new
2014-11-26 11:58:51 +01:00
@user = User.find(params[:user_id])
2014-11-25 15:27:12 +01:00
@order = @user.orders.build
2014-11-24 21:45:32 +01:00
@products = Product.all
2014-12-04 17:02:08 +01:00
@order_products = @order.order_products
@products.each do |p|
2014-12-04 19:28:21 +01:00
@order_products.build(product: p)
2014-12-04 17:02:08 +01:00
end
2014-11-06 18:56:00 +01:00
end
2014-11-23 21:12:31 +01:00
2014-11-06 18:56:00 +01:00
def create
2014-11-26 11:58:51 +01:00
@user = User.find(params[:user_id])
2014-12-04 19:28:21 +01:00
@order = @user.orders.build(order_params)
2014-12-04 17:02:08 +01:00
@products = Product.all
2014-12-05 16:15:07 +01:00
@order_products = @order.order_products
2014-12-04 19:28:21 +01:00
if @order.save
@user.pay(@order.price)
2014-12-04 19:28:21 +01:00
redirect_to root_path
else
render 'new'
2014-11-25 15:27:12 +01:00
end
2014-11-23 21:12:31 +01:00
end
2014-12-06 12:03:08 +01:00
def index
@user = User.find(params[:user_id])
@orders = @user.orders
end
2014-11-10 02:30:42 +01:00
private
def order_params
2014-12-05 16:15:07 +01:00
params.require(:order).permit(order_products_attributes: [:product_id, :count])
2014-11-10 02:30:42 +01:00
end
2014-11-06 18:56:00 +01:00
end