Last active
August 29, 2015 14:17
-
-
Save egrueter-dev/a070d397df15e6c72ae5 to your computer and use it in GitHub Desktop.
Handling Nested Routes - Categories/ Drinks Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This index handler lets you navigate to the following routes: | |
#category_drinks | GET | /categories/:category_id/drinks(.:format) | drinks#index | |
# drinks | GET | /drinks(.:format) | drinks#index | |
# and... | |
def index | |
if params.include?('category_id') | |
category = Category.find(params[:category_id]) | |
@drinks = Drink.where(category: category).page(params[:page]) | |
else | |
@drinks = Drink.page(params[:page]) | |
end | |
# if params.include?('category_id') | |
# @drinks = Drink.find(params[:category_id]) | |
# else | |
# @drinks = Drink.page(params[:page]) | |
# end | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.routes.draw do | |
root 'homes#index' | |
resources :drinks | |
resources :categories, only: [] do | |
resources :drinks | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
feature 'visitor views categorized drinks', %Q{ | |
As a visitor | |
I want to view drinks for a given category | |
So that I can make a drink that fits within that category | |
} do | |
# Acceptance Criteria | |
# | |
# * I can navigate to a drink listing for a given category | |
# * On the category listing, I should only see drinks that fall within that | |
# category | |
# * For nonexistant categories, a drink listing should not exist | |
# | |
let(:category) { FactoryGirl.create(:category) } | |
scenario 'drink in category is found on the category listing' do | |
drink = FactoryGirl.create(:drink, category: category) | |
visit '/categories/' + category.to_param + '/drinks' | |
expect(page).to have_content(drink.title) | |
end | |
scenario 'drink not in category is not found on the category listing' do | |
drink = FactoryGirl.create(:drink) | |
visit '/categories/' + category.to_param + '/drinks' | |
expect(page).to_not have_content(drink.title) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment