line_items_controller.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. :notice => 'Line item was successfully created.') }
  59. format.xml { render :xml => @line_item,
  60. :status => :created, :location => @line_item }
  61. else
  62. format.html { render :action => "new" }
  63. format.xml { render :xml => @line_item.errors,
  64. :status => :unprocessable_entity }
  65. end
  66. end
  67. end
  68. # PUT /line_items/1
  69. # PUT /line_items/1.xml
  70. def update
  71. @line_item = LineItem.find(params[:id])
  72. respond_to do |format|
  73. if @line_item.update_attributes(params[:line_item])
  74. format.html { redirect_to(@line_item, :notice => 'Line item was successfully updated.') }
  75. format.xml { head :ok }
  76. else
  77. format.html { render :action => "edit" }
  78. format.xml { render :xml => @line_item.errors, :status => :unprocessable_entity }
  79. end
  80. end
  81. end
  82. # DELETE /line_items/1
  83. # DELETE /line_items/1.xml
  84. def destroy
  85. @line_item = LineItem.find(params[:id])
  86. @line_item.destroy
  87. respond_to do |format|
  88. format.html { redirect_to(line_items_url) }
  89. format.xml { head :ok }
  90. end
  91. end
  92. end