carts_controller.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. @cart = Cart.find(params[:id])
  31. respond_to do |format|
  32. format.html # show.html.erb
  33. format.xml { render :xml => @cart }
  34. end
  35. end
  36. # GET /carts/new
  37. # GET /carts/new.xml
  38. def new
  39. @cart = Cart.new
  40. respond_to do |format|
  41. format.html # new.html.erb
  42. format.xml { render :xml => @cart }
  43. end
  44. end
  45. # GET /carts/1/edit
  46. def edit
  47. @cart = Cart.find(params[:id])
  48. end
  49. # POST /carts
  50. # POST /carts.xml
  51. def create
  52. @cart = Cart.new(params[:cart])
  53. respond_to do |format|
  54. if @cart.save
  55. format.html { redirect_to(@cart, :notice => 'Cart was successfully created.') }
  56. format.xml { render :xml => @cart, :status => :created, :location => @cart }
  57. else
  58. format.html { render :action => "new" }
  59. format.xml { render :xml => @cart.errors, :status => :unprocessable_entity }
  60. end
  61. end
  62. end
  63. # PUT /carts/1
  64. # PUT /carts/1.xml
  65. def update
  66. @cart = Cart.find(params[:id])
  67. respond_to do |format|
  68. if @cart.update_attributes(params[:cart])
  69. format.html { redirect_to(@cart, :notice => 'Cart was successfully updated.') }
  70. format.xml { head :ok }
  71. else
  72. format.html { render :action => "edit" }
  73. format.xml { render :xml => @cart.errors, :status => :unprocessable_entity }
  74. end
  75. end
  76. end
  77. # DELETE /carts/1
  78. # DELETE /carts/1.xml
  79. def destroy
  80. @cart = Cart.find(params[:id])
  81. @cart.destroy
  82. respond_to do |format|
  83. format.html { redirect_to(carts_url) }
  84. format.xml { head :ok }
  85. end
  86. end
  87. end