Fix controller orders create action

This commit is contained in:
Benjamin Cousaert 2014-12-04 19:28:21 +01:00
parent 95aaca802f
commit 9e598c5751

View file

@ -11,26 +11,38 @@ class OrdersController < ApplicationController
@order_products = @order.order_products @order_products = @order.order_products
@products.each do |p| @products.each do |p|
@order.order_products.build(product: p) @order_products.build(product: p)
end end
end end
def create def create
@user = User.find(params[:user_id]) @user = User.find(params[:user_id])
@order = @user.orders.build @order = @user.orders.build(order_params)
@products = Product.all @products = Product.all
@order_products = @order.order_products if @order.save
order_products = order_products_params
order_products.each do |k, v|
@order.order_products.create(product: Product.find(k), count: v[:count]) if v[:count].to_i > 0
end
redirect_to root_path
else
@order_products = @order.order_products
@products.each do |p| @products.each do |p|
@order.order_products.build(product: p) @order.order_products.build(product: p)
end
render 'new'
end end
render 'new'
end end
private private
def order_params def order_params
params.require(:order).permit(:products) params.require(:order).permit()
end
def order_products_params
params.require(:order).permit({:products => [:product_id, :count]})[:products]
end end
end end