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