carts_controller.rb 2.9 KB

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