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