carts_controller.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. class CartsController < ApplicationController
  10. # GET /carts
  11. # GET /carts.json
  12. def index
  13. @carts = Cart.all
  14. respond_to do |format|
  15. format.html # index.html.erb
  16. format.json { render json: @carts }
  17. end
  18. end
  19. # GET /carts/1
  20. # GET /carts/1.json
  21. def show
  22. @cart = Cart.find(params[:id])
  23. respond_to do |format|
  24. format.html # show.html.erb
  25. format.json { render json: @cart }
  26. end
  27. end
  28. # GET /carts/new
  29. # GET /carts/new.json
  30. def new
  31. @cart = Cart.new
  32. respond_to do |format|
  33. format.html # new.html.erb
  34. format.json { render json: @cart }
  35. end
  36. end
  37. # GET /carts/1/edit
  38. def edit
  39. @cart = Cart.find(params[:id])
  40. end
  41. # POST /carts
  42. # POST /carts.json
  43. def create
  44. @cart = Cart.new(params[:cart])
  45. respond_to do |format|
  46. if @cart.save
  47. format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
  48. format.json { render json: @cart, status: :created, location: @cart }
  49. else
  50. format.html { render action: "new" }
  51. format.json { render json: @cart.errors, status: :unprocessable_entity }
  52. end
  53. end
  54. end
  55. # PUT /carts/1
  56. # PUT /carts/1.json
  57. def update
  58. @cart = Cart.find(params[:id])
  59. respond_to do |format|
  60. if @cart.update_attributes(params[:cart])
  61. format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
  62. format.json { head :no_content }
  63. else
  64. format.html { render action: "edit" }
  65. format.json { render json: @cart.errors, status: :unprocessable_entity }
  66. end
  67. end
  68. end
  69. # DELETE /carts/1
  70. # DELETE /carts/1.json
  71. def destroy
  72. @cart = Cart.find(params[:id])
  73. @cart.destroy
  74. respond_to do |format|
  75. format.html { redirect_to carts_url }
  76. format.json { head :no_content }
  77. end
  78. end
  79. end