testy/app/controllers/products_controller.rb

52 lines
1.1 KiB
Ruby
Raw Permalink Normal View History

2026-05-17 11:12:43 +00:00
class ProductsController < ApplicationController
before_action :set_product, only: %i[ show update destroy ]
# GET /products
def index
@products = Product.all
render json: @products
end
# GET /products/1
def show
render json: @product
end
# POST /products
def create
@product = Product.new(product_params)
if @product.save
render json: @product, status: :created, location: @product
else
render json: @product.errors, status: :unprocessable_content
end
end
# PATCH/PUT /products/1
def update
if @product.update(product_params)
render json: @product
else
render json: @product.errors, status: :unprocessable_content
end
end
# DELETE /products/1
def destroy
@product.destroy!
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params.expect(:id))
end
# Only allow a list of trusted parameters through.
def product_params
params.expect(product: [ :name, :barcode ])
end
end