line_items_controller.rb 3.0 KB

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