products_controller.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. class ProductsController < ApplicationController
  2. before_action :set_product, only: [:show, :edit, :update, :destroy]
  3. # GET /products
  4. # GET /products.json
  5. def index
  6. @products = Product.all
  7. end
  8. # GET /products/1
  9. # GET /products/1.json
  10. def show
  11. end
  12. # GET /products/new
  13. def new
  14. @product = Product.new
  15. end
  16. # GET /products/1/edit
  17. def edit
  18. end
  19. # POST /products
  20. # POST /products.json
  21. def create
  22. @product = Product.new(product_params)
  23. respond_to do |format|
  24. if @product.save
  25. format.html { redirect_to @product, notice: 'Product was successfully created.' }
  26. format.json { render :show, status: :created, location: @product }
  27. else
  28. format.html { render :new }
  29. format.json { render json: @product.errors, status: :unprocessable_entity }
  30. end
  31. end
  32. end
  33. # PATCH/PUT /products/1
  34. # PATCH/PUT /products/1.json
  35. def update
  36. respond_to do |format|
  37. if @product.update(product_params)
  38. format.html { redirect_to @product, notice: 'Product was successfully updated.' }
  39. format.json { render :show, status: :ok, location: @product }
  40. else
  41. format.html { render :edit }
  42. format.json { render json: @product.errors, status: :unprocessable_entity }
  43. end
  44. end
  45. end
  46. # DELETE /products/1
  47. # DELETE /products/1.json
  48. def destroy
  49. @product.destroy
  50. respond_to do |format|
  51. format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
  52. format.json { head :no_content }
  53. end
  54. end
  55. private
  56. # Use callbacks to share common setup or constraints between actions.
  57. def set_product
  58. @product = Product.find(params[:id])
  59. end
  60. # Never trust parameters from the scary internet, only allow the white list through.
  61. def product_params
  62. params.require(:product).permit(:title, :description, :image_url, :price)
  63. end
  64. end