tap/app/controllers/products_controller.rb

46 lines
836 B
Ruby
Raw Normal View History

2014-11-24 21:45:32 +01:00
class ProductsController < ApplicationController
def new
@product = Product.new
end
def create
2014-11-25 13:41:22 +01:00
@product = Product.new(product_params)
2014-11-24 21:45:32 +01:00
if @product.save
2014-12-09 14:39:27 +01:00
redirect_to action: :index
2014-11-24 21:45:32 +01:00
else
2014-12-09 14:39:27 +01:00
render :new
2014-11-24 21:45:32 +01:00
end
end
2014-11-25 13:09:55 +01:00
def index
@products = Product.all
end
2014-11-24 21:45:32 +01:00
def edit
2014-11-25 13:09:55 +01:00
@product = Product.find(params[:id])
2014-11-24 21:45:32 +01:00
end
2014-11-25 13:41:22 +01:00
def update
@product = Product.find(params[:id])
if @product.update_attributes(product_params)
2014-12-09 14:42:50 +01:00
flash[:success] = "Succesfully updated product"
2014-12-09 14:39:27 +01:00
redirect_to action: :index
2014-11-25 13:41:22 +01:00
else
render 'edit'
end
end
2014-11-26 11:58:51 +01:00
def destroy
Product.find(params[:id]).destroy
2014-12-09 14:42:50 +01:00
flash[:success] = "Succesfully removed product"
2014-11-26 11:58:51 +01:00
redirect_to products_path
2014-11-24 21:45:32 +01:00
end
private
2014-11-25 13:41:22 +01:00
def product_params
2014-12-09 11:18:54 +01:00
params.require(:product).permit(:name, :price, :avatar)
2014-11-24 21:45:32 +01:00
end
end