require_dependency "fly_admin/application_controller" # Categories = Serialss module FlyAdmin class CategoriesController < ApplicationController before_filter :set_category, only: [:create, :update, :destroy] def index @categories = Category.all end def create if @category.save flash[:notice] = "Сериал успешно создан " redirect_to categories_path else render 'new' end end def new @category = Category.new end def edit @category = Category.find(params[:id]) end def update if @category.update_attributes(category_params) flash[:notice] = "Сериал обновлен" redirect_to categories_path else render action: "edit" end end def destroy @category = Category.find(params[:id]) @category.destroy! flash[:notice] = "Сериал успешно удален" redirect_to categories_path end def seasons @category = Category.find(params[:category_id]) @seasons = @category.seasons.each {|s| s.json_title = JSON.parse(s.title)["ru"]} end private def category_params params.require(:category).permit! end def set_category if params[:id] @category = Category.find(params[:id]) else redirect_to new_category_path, alert: "Укажите хотя бы 1 сериал и 1 сезон" and return unless params["category"].present? @category = Category.new category_params end @category.name = params[:name] end end end