Guest User

Categories Controller

a guest
Jan 10th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.11 KB | None | 0 0
  1. class CategoriesController < ApplicationController
  2.   before_action :set_category, only: [:show, :edit, :update, :destroy]
  3.  
  4.   def index
  5.     @categories = Category.all
  6.   end
  7.  
  8.   def show
  9.     @products = Product.where(category_id: [@category.subtree_ids])
  10.   end
  11.  
  12.   def new
  13.     @category = Category.new
  14.   end
  15.  
  16.   def create
  17.     @category = Category.new(category_params)
  18.     if @category.save
  19.       redirect_to categories_path, success: 'Категория успешно создана'
  20.     else
  21.       format.html { render :new }
  22.     end
  23.   end
  24.  
  25.   def edit; end
  26.  
  27.   def update
  28.     if
  29.       @category.update_attributes(category_params)
  30.       redirect_to categories_path, success: 'Категория успешно обновлена'
  31.     else
  32.       format.html { render :edit }
  33.     end
  34.   end
  35.  
  36.   def destroy
  37.     @category.destroy
  38.     redirect_to categories_path, success: 'Категория успешно удалена'
  39.   end
  40.  
  41.   private
  42.  
  43.   def set_category
  44.     @category = Category.find(params[:id])
  45.   end
  46.  
  47.   def category_params
  48.     params.require(:category).permit(:name, :parent_id)
  49.   end
  50. end
Advertisement
Add Comment
Please, Sign In to add comment