app/controllers/orders_controller.rb
# POST orders/confirm
# Function to parse saving to products.
POST orders/confirm
Function to parse saving to products.
POST orders / confirm Function to parse saving to products .
def confirm()
    params = conf_params
    @product_numbers = []
    @quantity_numbers = []
    @credit_card = 0
    Purchase.transaction do
      params.each do |k, v|
        if k == "credit_card"
          @credit_card = v
          next
        end
        @product_numbers.append(v["product"])
        @quantity_numbers.append(v["quantity"])
        v[:user] = current_user.id
        v = v.permit(:user, :quantity, :product)
        @insert_purchase = Purchase.new(v)
        if @insert_purchase.save
          next
        else
          flash[:alert] = @insert_purchase.errors.full_messages.to_sentence
          index()
          render action: :index
        end
      end
      Cart.where(:user_id => current_user.id).destroy_all
    end
  rescue => e
    redirect_to "/orders", flash: {notice: "Failed to buy cart."}
  end
def confirm ( ) params = conf_params @product_numbers = [ ] @quantity_numbers = [ ] @credit_card = 0 Purchase . transaction do params . each do | k , v | if k == "credit_card" @credit_card = v next end @product_numbers . append ( v [ "product" ] ) @quantity_numbers . append ( v [ "quantity" ] ) v [ :user ] = current_user . id v = v . permit ( :user , :quantity , :product ) @insert_purchase = Purchase . new ( v ) if @insert_purchase . save next else flash [ :alert ] = @insert_purchase . errors . full_messages . to_sentence index ( ) render action : :index end end Cart . where ( :user_id => current_user . id ) . destroy_all end rescue => e redirect_to "/orders" , flash : { notice : "Failed to buy cart." } end
POST orders/confirm
Function to parse saving to products.
POST orders / confirm Function to parse saving to products .
# Potentially dangerous without checking user id