25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

83 satır
2.0 KiB

  1. class CartsController < ApplicationController
  2. before_action :set_cart, only: [:show, :edit, :update, :destroy]
  3. # QUESTION: How do we call this with call?
  4. rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
  5. # GET /carts
  6. # GET /carts.json
  7. def index
  8. @carts = Cart.all
  9. end
  10. # GET /carts/1
  11. # GET /carts/1.json
  12. def show
  13. end
  14. # GET /carts/new
  15. def new
  16. @cart = Cart.new
  17. end
  18. # GET /carts/1/edit
  19. def edit
  20. end
  21. # POST /carts
  22. # POST /carts.json
  23. def create
  24. @cart = Cart.new(cart_params)
  25. respond_to do |format|
  26. if @cart.save
  27. format.html { redirect_to store_index_url }
  28. format.json { render :show, status: :created, location: @cart }
  29. else
  30. format.html { render :new }
  31. format.json { render json: @cart.errors, status: :unprocessable_entity }
  32. end
  33. end
  34. end
  35. # PATCH/PUT /carts/1
  36. # PATCH/PUT /carts/1.json
  37. def update
  38. respond_to do |format|
  39. if @cart.update(cart_params)
  40. format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
  41. format.json { render :show, status: :ok, location: @cart }
  42. else
  43. format.html { render :edit }
  44. format.json { render json: @cart.errors, status: :unprocessable_entity }
  45. end
  46. end
  47. end
  48. # DELETE /carts/1
  49. # DELETE /carts/1.json
  50. def destroy
  51. @cart.destroy if @cart.id == session[:cart_id]
  52. session[:cart_id] = nil
  53. respond_to do |format|
  54. format.html { redirect_to store_index_url }
  55. format.json { head :no_content }
  56. end
  57. end
  58. private
  59. # Use callbacks to share common setup or constraints between actions.
  60. def set_cart
  61. @cart = Cart.find(params[:id])
  62. end
  63. # Never trust parameters from the scary internet, only allow the white list through.
  64. def cart_params
  65. params.fetch(:cart, {})
  66. end
  67. def invalid_cart
  68. logger.error "Attempt to access invalid cart #{params[:id]}"
  69. redirect_to store_index_url, notice: 'Invalid cart'
  70. end
  71. end