tap/app/controllers/orders_controller.rb

50 lines
1.1 KiB
Ruby
Raw Normal View History

2014-11-06 18:56:00 +01:00
class OrdersController < ApplicationController
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
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
2014-12-10 01:03:22 +01:00
flash[:success] = "Ordered things! Get your stuff!"
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
2014-12-10 01:03:22 +01:00
@users_by_name = User.all.order(:name)
@users_by_order = User.all.order(:orders_count).reverse_order
2014-12-06 12:03:08 +01:00
end
def quickpay
@user = User.find(params[:user_id])
order = @user.orders.build
order.products << @user.dagschotel
if order.save
flash[:success] = "Quick pay succeeded"
else
flash[:error] = "Quick pay went wrong ... Sorry!"
end
redirect_to root_path
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