line_items_controller.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #---
  10. # Excerpted from "Agile Web Development with Rails, 4rd Ed.",
  11. # published by The Pragmatic Bookshelf.
  12. # Copyrights apply to this code. It may not be used to create training material,
  13. # courses, books, articles, and the like. Contact us if you are in doubt.
  14. # We make no guarantees that this code is fit for any purpose.
  15. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  16. #---
  17. class LineItemsController < ApplicationController
  18. # GET /line_items
  19. # GET /line_items.xml
  20. def index
  21. @line_items = LineItem.all
  22. respond_to do |format|
  23. format.html # index.html.erb
  24. format.xml { render :xml => @line_items }
  25. end
  26. end
  27. # GET /line_items/1
  28. # GET /line_items/1.xml
  29. def show
  30. @line_item = LineItem.find(params[:id])
  31. respond_to do |format|
  32. format.html # show.html.erb
  33. format.xml { render :xml => @line_item }
  34. end
  35. end
  36. # GET /line_items/new
  37. # GET /line_items/new.xml
  38. def new
  39. @line_item = LineItem.new
  40. respond_to do |format|
  41. format.html # new.html.erb
  42. format.xml { render :xml => @line_item }
  43. end
  44. end
  45. # GET /line_items/1/edit
  46. def edit
  47. @line_item = LineItem.find(params[:id])
  48. end
  49. # POST /line_items
  50. # POST /line_items.xml
  51. def create
  52. @cart = current_cart
  53. product = Product.find(params[:product_id])
  54. @line_item = @cart.add_product(product.id)
  55. respond_to do |format|
  56. if @line_item.save
  57. format.html { redirect_to(@line_item.cart) }
  58. format.xml { render :xml => @line_item,
  59. :status => :created, :location => @line_item }
  60. else
  61. format.html { render :action => "new" }
  62. format.xml { render :xml => @line_item.errors,
  63. :status => :unprocessable_entity }
  64. end
  65. end
  66. end
  67. # PUT /line_items/1
  68. # PUT /line_items/1.xml
  69. def update
  70. @line_item = LineItem.find(params[:id])
  71. respond_to do |format|
  72. if @line_item.update_attributes(params[:line_item])
  73. format.html { redirect_to(@line_item, :notice => 'Line item was successfully updated.') }
  74. format.xml { head :ok }
  75. else
  76. format.html { render :action => "edit" }
  77. format.xml { render :xml => @line_item.errors, :status => :unprocessable_entity }
  78. end
  79. end
  80. end
  81. # DELETE /line_items/1
  82. # DELETE /line_items/1.xml
  83. def destroy
  84. @line_item = LineItem.find(params[:id])
  85. @line_item.destroy
  86. respond_to do |format|
  87. format.html { redirect_to(line_items_url) }
  88. format.xml { head :ok }
  89. end
  90. end
  91. end