123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #---
- # 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.
- #---
- #---
- # Excerpted from "Agile Web Development with Rails, 4rd Ed.",
- # 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
- # GET /orders
- # GET /orders.xml
- def index
- @orders = Order.all
- respond_to do |format|
- format.html # index.html.erb
- format.xml { render :xml => @orders }
- end
- end
- # GET /orders/1
- # GET /orders/1.xml
- def show
- @order = Order.find(params[:id])
- respond_to do |format|
- format.html # show.html.erb
- format.xml { render :xml => @order }
- end
- end
- # GET /orders/new
- # GET /orders/new.xml
- def new
- @cart = current_cart
- if @cart.line_items.empty?
- redirect_to store_url, :notice => "Your cart is empty"
- return
- end
- @order = Order.new
- respond_to do |format|
- format.html # new.html.erb
- format.xml { render :xml => @order }
- end
- end
- # GET /orders/1/edit
- def edit
- @order = Order.find(params[:id])
- end
- # POST /orders
- # POST /orders.xml
- def create
- @order = Order.new(params[:order])
- @order.add_line_items_from_cart(current_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.xml { render :xml => @order, :status => :created,
- :location => @order }
- else
- format.html { render :action => "new" }
- format.xml { render :xml => @order.errors,
- :status => :unprocessable_entity }
- end
- end
- end
- # PUT /orders/1
- # PUT /orders/1.xml
- def update
- @order = Order.find(params[:id])
- respond_to do |format|
- if @order.update_attributes(params[:order])
- format.html { redirect_to(@order, :notice => 'Order was successfully updated.') }
- format.xml { head :ok }
- else
- format.html { render :action => "edit" }
- format.xml { render :xml => @order.errors, :status => :unprocessable_entity }
- end
- end
- end
- # DELETE /orders/1
- # DELETE /orders/1.xml
- def destroy
- @order = Order.find(params[:id])
- @order.destroy
- respond_to do |format|
- format.html { redirect_to(orders_url) }
- format.xml { head :ok }
- end
- end
- end
|