Sha256: c5bf3707493344e7078c4dbc853b2422adee17b3c813d2455656f80792e00fdb

Contents?: true

Size: 1.66 KB

Versions: 4

Compression:

Stored size: 1.66 KB

Contents

require_dependency "guts/application_controller"

module Guts
  # Categories controller
  class CategoriesController < ApplicationController
    before_action :set_category, only: [:show, :edit, :update, :destroy]

    # Displays a list of categories
    def index
      @categories = Category.all
    end

    # Shows details about a single category
    def show
    end

    # Creation of a new category
    def new
      @category = Category.new
    end

    # Editing for a category
    def edit
    end

    # Creates a category through post
    # @note Redirects to #index if successfull or re-renders #new if not
    def create
      @category = Category.new category_params

      if @category.save
        redirect_to categories_path, notice: "Category was successfully created."
      else
        render :new
      end
    end

    # Updates a category through patch
    # @note Redirects to #index if successfull or re-renders #edit if not
    def update
      if @category.update category_params
        redirect_to categories_path, notice: "Category was successfully updated."
      else
        render :edit
      end
    end

    # Destroys a category
    # @note Redirects to #index on success
    def destroy
      @category.destroy
      redirect_to categories_path, notice: "Category was successfully destroyed."
    end

    private
    # Sets a category from the database using `id` param
    # @note This is a `before_action` callback
    # @private
    def set_category
      @category = Category.find params[:id]
    end

    # Permits category params from forms
    # @private
    def category_params
      params.require(:category).permit(:title, :slug)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
guts-1.0.8 app/controllers/guts/categories_controller.rb
guts-1.0.7 app/controllers/guts/categories_controller.rb
guts-1.0.5 app/controllers/guts/categories_controller.rb
guts-1.0.3 app/controllers/guts/categories_controller.rb