Skip to content

Instantly share code, notes, and snippets.

@Phoenix23A
Created February 22, 2017 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phoenix23A/2f169dd9b3b1feeef5a71179f2813401 to your computer and use it in GitHub Desktop.
Save Phoenix23A/2f169dd9b3b1feeef5a71179f2813401 to your computer and use it in GitHub Desktop.
lass ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
respond_to :json, :html
# GET /products
# GET /products.json
def index
if params[:q]
search_term = params[:q]
@products = Product.where("name LIKE ?", "%#{search_term}%")
else
@products = Product.all
end
respond_with @products
end
# GET /products/1
# GET /products/1.json
def show
@comments = @product.comments.order("created_at DESC").paginate(:page => params[:page], :per_page => 5)
end
# GET /products/new
def new
@product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :description, :image_url, :color, :price)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment