84 lines
2.4 KiB
Ruby
84 lines
2.4 KiB
Ruby
class CategoriesController < ApplicationController
|
|
before_action :set_category, only: %i[ show edit update destroy ]
|
|
|
|
# GET /categories or /categories.json
|
|
def index
|
|
@categories = Category.all.order(:name)
|
|
end
|
|
|
|
# GET /categories/1 or /categories/1.json
|
|
def show
|
|
@category = Category.find(params[:id])
|
|
|
|
# Basis-Abfrage: Alle Items dieser Kategorie laden
|
|
@items = @category.items.includes(:user, :room).order(created_at: :desc)
|
|
|
|
if params[:query].present?
|
|
query_str = "%#{params[:query]}%"
|
|
@items = @items.where(
|
|
"items.name LIKE :q OR items.sku LIKE :q OR items.serial_number LIKE :q OR items.sticker_id LIKE :q",
|
|
q: query_str
|
|
)
|
|
end
|
|
end
|
|
|
|
# GET /categories/new
|
|
def new
|
|
@category = Category.new
|
|
end
|
|
|
|
# GET /categories/1/edit
|
|
def edit
|
|
end
|
|
|
|
# POST /categories or /categories.json
|
|
def create
|
|
@category = Category.new(category_params)
|
|
|
|
respond_to do |format|
|
|
if @category.save
|
|
format.html { redirect_to @category, notice: "Category was successfully created." }
|
|
format.json { render :show, status: :created, location: @category }
|
|
else
|
|
format.html { render :new, status: :unprocessable_content }
|
|
format.json { render json: @category.errors, status: :unprocessable_content }
|
|
end
|
|
end
|
|
end
|
|
|
|
# PATCH/PUT /categories/1 or /categories/1.json
|
|
def update
|
|
respond_to do |format|
|
|
if @category.update(category_params)
|
|
format.html { redirect_to @category, notice: "Category was successfully updated.", status: :see_other }
|
|
format.json { render :show, status: :ok, location: @category }
|
|
else
|
|
format.html { render :edit, status: :unprocessable_content }
|
|
format.json { render json: @category.errors, status: :unprocessable_content }
|
|
end
|
|
end
|
|
end
|
|
|
|
# DELETE /categories/1 or /categories/1.json
|
|
def destroy
|
|
@category.destroy!
|
|
|
|
respond_to do |format|
|
|
format.html { redirect_to categories_path, notice: "Category was successfully destroyed.", status: :see_other }
|
|
format.json { head :no_content }
|
|
end
|
|
end
|
|
|
|
private
|
|
# Use callbacks to share common setup or constraints between actions.
|
|
def set_category
|
|
@category = Category.find(params.expect(:id))
|
|
end
|
|
|
|
|
|
# Only allow a list of trusted parameters through.
|
|
def category_params
|
|
params.require(:category).permit(:name, :description)
|
|
end
|
|
end
|