line_items_controller.rb 2.8 KB

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