carts_controller.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 CartsController < ApplicationController
  18. # GET /carts
  19. # GET /carts.xml
  20. def index
  21. @carts = Cart.all
  22. respond_to do |format|
  23. format.html # index.html.erb
  24. format.xml { render :xml => @carts }
  25. end
  26. end
  27. # GET /carts/1
  28. # GET /carts/1.xml
  29. def show
  30. begin
  31. @cart = Cart.find(params[:id])
  32. rescue ActiveRecord::RecordNotFound
  33. logger.error "Attempt to access invalid cart #{params[:id]}"
  34. redirect_to store_url, :notice => 'Invalid cart'
  35. else
  36. respond_to do |format|
  37. format.html # show.html.erb
  38. format.xml { render :xml => @cart }
  39. end
  40. end
  41. end
  42. # GET /carts/new
  43. # GET /carts/new.xml
  44. def new
  45. @cart = Cart.new
  46. respond_to do |format|
  47. format.html # new.html.erb
  48. format.xml { render :xml => @cart }
  49. end
  50. end
  51. # GET /carts/1/edit
  52. def edit
  53. @cart = Cart.find(params[:id])
  54. end
  55. # POST /carts
  56. # POST /carts.xml
  57. def create
  58. @cart = Cart.new(params[:cart])
  59. respond_to do |format|
  60. if @cart.save
  61. format.html { redirect_to(@cart, :notice => 'Cart was successfully created.') }
  62. format.xml { render :xml => @cart, :status => :created, :location => @cart }
  63. else
  64. format.html { render :action => "new" }
  65. format.xml { render :xml => @cart.errors, :status => :unprocessable_entity }
  66. end
  67. end
  68. end
  69. # PUT /carts/1
  70. # PUT /carts/1.xml
  71. def update
  72. @cart = Cart.find(params[:id])
  73. respond_to do |format|
  74. if @cart.update_attributes(params[:cart])
  75. format.html { redirect_to(@cart, :notice => 'Cart was successfully updated.') }
  76. format.xml { head :ok }
  77. else
  78. format.html { render :action => "edit" }
  79. format.xml { render :xml => @cart.errors, :status => :unprocessable_entity }
  80. end
  81. end
  82. end
  83. # DELETE /carts/1
  84. # DELETE /carts/1.xml
  85. def destroy
  86. @cart = current_cart
  87. @cart.destroy
  88. session[:cart_id] = nil
  89. respond_to do |format|
  90. format.html { redirect_to(store_url,
  91. :notice => 'Your cart is currently empty') }
  92. format.xml { head :ok }
  93. end
  94. end
  95. end