From c0400700fca7539568b70403f661a85004ac12a8 Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Sun, 12 Mar 2017 15:15:15 -0400 Subject: [PATCH] working on categories for schools --- app/controllers/categories_controller.rb | 8 +++++++- config/routes.rb | 1 + spec/controllers/categories_controller_spec.rb | 12 ++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 5ff956aa..940fc4a4 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -1,4 +1,5 @@ class CategoriesController < ApplicationController + before_action :set_school, only: [:show] before_action :set_category, only: [:show, :edit, :update, :destroy] # GET /categories @@ -62,7 +63,12 @@ class CategoriesController < ApplicationController end private - # Use callbacks to share common setup or constraints between actions. + def set_school + redirect_to root_path and return false unless params.include?(:school_id) + @school = School.find(params[:school_id]) + redirect_to root_path and return false if @school.nil? + end + def set_category @category = Category.find(params[:id]) end diff --git a/config/routes.rb b/config/routes.rb index a4b344fe..7f64348f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,6 +13,7 @@ Rails.application.routes.draw do end end resources :schedules + resources :categories, only: [:show] end # resources :attempts, only: [:get, :update] diff --git a/spec/controllers/categories_controller_spec.rb b/spec/controllers/categories_controller_spec.rb index 074175e7..edcf84be 100644 --- a/spec/controllers/categories_controller_spec.rb +++ b/spec/controllers/categories_controller_spec.rb @@ -45,10 +45,18 @@ RSpec.describe CategoriesController, type: :controller do end describe "GET #show" do - it "assigns the requested category as @category" do + it "assigns the requested school and category as @school and @category" do + school = School.create! name: 'School' category = Category.create! valid_attributes - get :show, params: {id: category.to_param}, session: valid_session + get :show, params: {school_id: school.id, id: category.to_param}, session: valid_session expect(assigns(:category)).to eq(category) + expect(assigns(:school)).to eq(school) + end + + it "redirects to root_path if school is not provided" do + category = Category.create! valid_attributes + get :show, params: {id: category.to_param}, session: valid_session + expect(response).to redirect_to(root_path) end end