form_controller.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 FormController < ApplicationController
  18. def index
  19. list
  20. render_action 'list'
  21. end
  22. def list
  23. @product_pages, @products = paginate :product, :per_page => 10
  24. end
  25. def show
  26. @product = Product.find(params[:id])
  27. end
  28. def new
  29. @product = Product.new
  30. end
  31. def create
  32. @product = Product.new(params[:product])
  33. if @product.save
  34. flash['notice'] = 'Product was successfully created.'
  35. redirect_to :action => 'list'
  36. else
  37. render_action 'new'
  38. end
  39. end
  40. def edit
  41. @product = Product.find(params[:id])
  42. end
  43. def update
  44. @product = Product.find(params[:id])
  45. if @product.update_attributes(params[:product])
  46. flash['notice'] = 'Product was successfully updated.'
  47. redirect_to :action => 'show', :id => @product
  48. else
  49. render_action 'edit'
  50. end
  51. end
  52. def destroy
  53. Product.find(params[:id]).destroy
  54. redirect_to :action => 'list'
  55. end
  56. end