carts_controller.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. before_action :set_cart, only: [:show, :edit, :update, :destroy]
  11. # GET /carts
  12. # GET /carts.json
  13. def index
  14. @carts = Cart.all
  15. end
  16. # GET /carts/1
  17. # GET /carts/1.json
  18. def show
  19. end
  20. # GET /carts/new
  21. def new
  22. @cart = Cart.new
  23. end
  24. # GET /carts/1/edit
  25. def edit
  26. end
  27. # POST /carts
  28. # POST /carts.json
  29. def create
  30. @cart = Cart.new(cart_params)
  31. respond_to do |format|
  32. if @cart.save
  33. format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
  34. format.json { render :show, status: :created, location: @cart }
  35. else
  36. format.html { render :new }
  37. format.json { render json: @cart.errors, status: :unprocessable_entity }
  38. end
  39. end
  40. end
  41. # PATCH/PUT /carts/1
  42. # PATCH/PUT /carts/1.json
  43. def update
  44. respond_to do |format|
  45. if @cart.update(cart_params)
  46. format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
  47. format.json { render :show, status: :ok, location: @cart }
  48. else
  49. format.html { render :edit }
  50. format.json { render json: @cart.errors, status: :unprocessable_entity }
  51. end
  52. end
  53. end
  54. # DELETE /carts/1
  55. # DELETE /carts/1.json
  56. def destroy
  57. @cart.destroy
  58. respond_to do |format|
  59. format.html { redirect_to carts_url, notice: 'Cart was successfully destroyed.' }
  60. format.json { head :no_content }
  61. end
  62. end
  63. private
  64. # Use callbacks to share common setup or constraints between actions.
  65. def set_cart
  66. @cart = Cart.find(params[:id])
  67. end
  68. # Never trust parameters from the scary internet, only allow the white list through.
  69. def cart_params
  70. params[:cart]
  71. end
  72. end