misadventure/app/controllers/categories_controller.rb

49 lines
936 B
Ruby

class CategoriesController < ApplicationController
allow_unauthenticated_access only: %i[ index show ]
before_action :set_category, only: %i[ show edit update destroy ]
def index
@categories = Category.all
end
def show
@clips = Clip.where("category_id = ?", params[:id])
end
def new
@category = Category.new
end
def create
@category = Category.new(category_params)
if @category.save
redirect_to @category
else
render :new, status: :unprocessable_entity
end
end
def edit
end
def update
if @category.update(category_params)
redirect_to @category
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@category.destroy
redirect_to categories_path
end
private
def set_category
@category = Category.find(params[:id])
end
def category_params
params.expect(category: [ :name ])
end
end