line_items_controller.rb 2.5 KB

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