Advertisement
Guest User

Untitled

a guest
May 14th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.93 KB | None | 0 0
  1. class ProductsController < ApplicationController
  2.   before_action :set_product, only: [:show, :edit, :update, :destroy]
  3.  
  4.   # GET /products
  5.   # GET /products.json
  6.   def index
  7.     @products = Product.all
  8.   end
  9.  
  10.   # GET /products/1
  11.   # GET /products/1.json
  12.   def show
  13.   end
  14.  
  15.   # GET /products/new
  16.   def new
  17.     @product = Product.new
  18.   end
  19.  
  20.   # GET /products/1/edit
  21.   def edit
  22.   end
  23.  
  24.   # POST /products
  25.   # POST /products.json
  26.   def create
  27.     @product = Product.new(product_params)
  28.  
  29.     respond_to do |format|
  30.       if @product.save
  31.         format.html { redirect_to @product, notice: 'Product was successfully created.' }
  32.         format.json { render :show, status: :created, location: @product }
  33.       else
  34.         format.html { render :new }
  35.         format.json { render json: @product.errors, status: :unprocessable_entity }
  36.       end
  37.     end
  38.   end
  39.  
  40.   # PATCH/PUT /products/1
  41.   # PATCH/PUT /products/1.json
  42.   def update
  43.     respond_to do |format|
  44.       if @product.update(product_params)
  45.         format.html { redirect_to @product, notice: 'Product was successfully updated.' }
  46.         format.json { render :show, status: :ok, location: @product }
  47.       else
  48.         format.html { render :edit }
  49.         format.json { render json: @product.errors, status: :unprocessable_entity }
  50.       end
  51.     end
  52.   end
  53.  
  54.   # DELETE /products/1
  55.   # DELETE /products/1.json
  56.   def destroy
  57.     @product.destroy
  58.     respond_to do |format|
  59.       format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
  60.       format.json { head :no_content }
  61.     end
  62.   end
  63.  
  64.   private
  65.     # Use callbacks to share common setup or constraints between actions.
  66.     def set_product
  67.       @product = Product.find(params[:id])
  68.     end
  69.  
  70.     # Never trust parameters from the scary internet, only allow the white list through.
  71.     def product_params
  72.       params.require(:product).permit(:name, :description)
  73.     end
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement