orders_controller.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #---
  2. # Excerpted from "Agile Web Development with Rails",
  3. # published by The Pragmatic Bookshelf.
  4. # Copyrights apply to this code. It may not be used to create training material,
  5. # courses, books, articles, and the like. Contact us if you are in doubt.
  6. # We make no guarantees that this code is fit for any purpose.
  7. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  8. #---
  9. #---
  10. # Excerpted from "Agile Web Development with Rails, 4rd Ed.",
  11. # published by The Pragmatic Bookshelf.
  12. # Copyrights apply to this code. It may not be used to create training material,
  13. # courses, books, articles, and the like. Contact us if you are in doubt.
  14. # We make no guarantees that this code is fit for any purpose.
  15. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  16. #---
  17. class OrdersController < ApplicationController
  18. # GET /orders
  19. # GET /orders.xml
  20. def index
  21. @orders = Order.all
  22. respond_to do |format|
  23. format.html # index.html.erb
  24. format.xml { render :xml => @orders }
  25. end
  26. end
  27. # GET /orders/1
  28. # GET /orders/1.xml
  29. def show
  30. @order = Order.find(params[:id])
  31. respond_to do |format|
  32. format.html # show.html.erb
  33. format.xml { render :xml => @order }
  34. end
  35. end
  36. # GET /orders/new
  37. # GET /orders/new.xml
  38. def new
  39. @cart = current_cart
  40. if @cart.line_items.empty?
  41. redirect_to store_url, :notice => "Your cart is empty"
  42. return
  43. end
  44. @order = Order.new
  45. respond_to do |format|
  46. format.html # new.html.erb
  47. format.xml { render :xml => @order }
  48. end
  49. end
  50. # GET /orders/1/edit
  51. def edit
  52. @order = Order.find(params[:id])
  53. end
  54. # POST /orders
  55. # POST /orders.xml
  56. def create
  57. @order = Order.new(params[:order])
  58. @order.add_line_items_from_cart(current_cart)
  59. respond_to do |format|
  60. if @order.save
  61. Cart.destroy(session[:cart_id])
  62. session[:cart_id] = nil
  63. format.html { redirect_to(store_url, :notice =>
  64. 'Thank you for your order.') }
  65. format.xml { render :xml => @order, :status => :created,
  66. :location => @order }
  67. else
  68. format.html { render :action => "new" }
  69. format.xml { render :xml => @order.errors,
  70. :status => :unprocessable_entity }
  71. end
  72. end
  73. end
  74. # PUT /orders/1
  75. # PUT /orders/1.xml
  76. def update
  77. @order = Order.find(params[:id])
  78. respond_to do |format|
  79. if @order.update_attributes(params[:order])
  80. format.html { redirect_to(@order, :notice => 'Order was successfully updated.') }
  81. format.xml { head :ok }
  82. else
  83. format.html { render :action => "edit" }
  84. format.xml { render :xml => @order.errors, :status => :unprocessable_entity }
  85. end
  86. end
  87. end
  88. # DELETE /orders/1
  89. # DELETE /orders/1.xml
  90. def destroy
  91. @order = Order.find(params[:id])
  92. @order.destroy
  93. respond_to do |format|
  94. format.html { redirect_to(orders_url) }
  95. format.xml { head :ok }
  96. end
  97. end
  98. end