tap/app/controllers/orders_controller.rb

45 lines
928 B
Ruby
Raw Normal View History

2014-11-06 17:56:00 +00:00
class OrdersController < ApplicationController
2014-11-25 14:27:12 +00:00
def overview
2014-11-23 20:12:31 +00:00
@users = User.all
2014-11-06 17:56:00 +00:00
end
2014-11-10 01:30:42 +00:00
2014-11-25 14:27:12 +00:00
def new
2014-11-26 10:58:51 +00:00
@user = User.find(params[:user_id])
2014-11-25 14:27:12 +00:00
@order = @user.orders.build
2014-11-24 20:45:32 +00:00
@products = Product.all
2014-12-04 16:02:08 +00:00
@order_products = @order.order_products
@products.each do |p|
2014-12-04 18:28:21 +00:00
@order_products.build(product: p)
2014-12-04 16:02:08 +00:00
end
2014-11-06 17:56:00 +00:00
end
2014-11-23 20:12:31 +00:00
2014-11-06 17:56:00 +00:00
def create
2014-11-26 10:58:51 +00:00
@user = User.find(params[:user_id])
2014-12-04 18:28:21 +00:00
@order = @user.orders.build(order_params)
2014-12-04 16:02:08 +00:00
@products = Product.all
@products.each do |p|
@order.order_products.build(product: p, count: order_products_params[p.id.to_s][:count])
end
2014-12-04 18:28:21 +00:00
if @order.save
redirect_to root_path
else
@order_products = @order.order_products
render 'new'
2014-11-25 14:27:12 +00:00
end
2014-11-23 20:12:31 +00:00
end
2014-11-10 01:30:42 +00:00
private
def order_params
2014-12-04 18:28:21 +00:00
params.require(:order).permit()
end
def order_products_params
params.require(:order).permit({:products => [:product_id, :count]})[:products]
2014-11-10 01:30:42 +00:00
end
2014-11-06 17:56:00 +00:00
end