1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #---
- # Excerpted from "Agile Web Development with Rails",
- # published by The Pragmatic Bookshelf.
- # Copyrights apply to this code. It may not be used to create training material,
- # courses, books, articles, and the like. Contact us if you are in doubt.
- # We make no guarantees that this code is fit for any purpose.
- # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
- #---
- class OrdersController < ApplicationController
- include CurrentCart
- before_action :set_cart, only: [:new, :create]
- before_action :set_order, only: [:show, :edit, :update, :destroy]
- # GET /orders
- # GET /orders.json
- def index
- @orders = Order.all
- end
- # GET /orders/1
- # GET /orders/1.json
- def show
- end
- # GET /orders/new
- def new
- if @cart.line_items.empty?
- redirect_to store_url, notice: "Your cart is empty"
- return
- end
- @order = Order.new
- end
- # GET /orders/1/edit
- def edit
- end
- # POST /orders
- # POST /orders.json
- def create
- @order = Order.new(order_params)
- @order.add_line_items_from_cart(@cart)
- respond_to do |format|
- if @order.save
- Cart.destroy(session[:cart_id])
- session[:cart_id] = nil
-
- format.html { redirect_to store_url, notice:
- 'Thank you for your order.' }
- format.json { render action: 'show', status: :created,
- location: @order }
-
- else
- format.html { render action: 'new' }
- format.json { render json: @order.errors,
- status: :unprocessable_entity }
- end
- end
- end
- # PATCH/PUT /orders/1
- # PATCH/PUT /orders/1.json
- def update
- respond_to do |format|
- if @order.update(order_params)
- format.html { redirect_to @order, notice: 'Order was successfully updated.' }
- format.json { head :no_content }
- else
- format.html { render action: 'edit' }
- format.json { render json: @order.errors, status: :unprocessable_entity }
- end
- end
- end
- # DELETE /orders/1
- # DELETE /orders/1.json
- def destroy
- @order.destroy
- respond_to do |format|
- format.html { redirect_to orders_url }
- format.json { head :no_content }
- end
- end
- private
- # Use callbacks to share common setup or constraints between actions.
- def set_order
- @order = Order.find(params[:id])
- end
- # Never trust parameters from the scary internet, only allow the white list through.
- def order_params
- params.require(:order).permit(:name, :address, :email, :pay_type)
- end
- #...
- end
|