SlideShare a Scribd company logo
1 of 17
Download to read offline
Dynamic LAYOUT 
NAVIGATION 
@PAREIDOLIAX
PROBLEM
STATIC SITE NAVIGATION 
WITH BOOTSTRAP
BOOTSTRAP SOURCE 
<nav> 
<ul class="nav nav-pills"> 
<li class="inactive"><a href="/">Home</a></li> 
<li class="active"><a href="/portfolio">Portfolio</a></li> 
<li class="inactive"><a href="/about">About</a></li> 
<li class="inactive"><a href="/contact">Contact</a></li> 
</ul> 
</nav>
REQUIREMENTS
REQUIREMENTS 
We need to be able to check if we are rendering the destination of the 
link and we then need to be able to set the class accordingly.
REQUIREMENTS 
rake routes 
Prefix Verb URI Pattern Controller#Action 
portfolio GET /portfolio(.:format) welcome#portfolio 
about GET /about(.:format) welcome#about 
contact GET /contact(.:format) welcome#contact 
shopify GET /shopify(.:format) welcome#shopify 
root GET / welcome#home 
link_to("About", about_path)
REQUIREMENTS 
▸ need to determine current_path 
▸ need to compare it to the link_path 
▸ neet to set css_class_active_for(link_path) 
<%= content_tag :li, link_to("About", about_path), class: "#{css_class_active_for(about_path)}" %>
TARGET CODE 
_navbar.html.erb 
<nav> 
<ul class="nav nav-pills"> 
<%= content_tag :li, link_to("Home", root_path), class: "#{css_class_active_for(root_path)}" %> 
<%= content_tag :li, link_to("Portfolio", portfolio_path), class: "#{css_class_active_for(portfolio_path)}" %> 
<%= content_tag :li, link_to("About", about_path), class: "#{css_class_active_for(about_path)}" %> 
<%= content_tag :li, link_to("Contact", contact_path), class: "#{css_class_active_for(contact_path)}" %> 
</ul> 
</nav>
TEST DRIVEN 
DEVELOPMENT 
WITH RSPEC
TEST 
THEN 
CODE
TEST FOR current_path HELPER 
application_helper_spec.rb 
# helper is an instance of ActionView::Base configured with the 
# ApplicationHelper and all of Rails' built-in helpers 
require "spec_helper" 
describe ApplicationHelper do 
context '#current_path' do 
it "returns #request.env['PATH_INFO']" do 
helper.request.env['PATH_INFO'] = '/welcome' 
helper.current_path.should eq('/welcome') 
end 
end 
end
CODE FOR current_path HELPER 
application_helper.rb 
module ApplicationHelper 
def current_path 
request.env['PATH_INFO'] 
end 
end
TEST OF css_class_active_for 
HELPER 
layout_helper_spec.rb 
require "spec_helper" 
describe LayoutHelper do 
context "#css_class_active_for" do 
it "returns 'active' if the current path matches link path" do 
helper.stub(:current_path).and_return('/about') 
helper.stub(:about_path).and_return('/about') 
expect(helper.css_class_active_for(about_path)).to match /active/ 
end 
it "returns 'inactive' if the current path does not match the link path" do 
helper.stub(:current_path).and_return('/') 
helper.stub(:about_path).and_return('/about') 
expect(helper.css_class_active_for(about_path)).to match /inactive/ 
end 
end 
end
require "spec_helper" 
describe LayoutHelper do 
context "#css_class_active_for" do 
it "returns 'active' if the current path matches link path" do 
helper.stub(:current_path).and_return('/about') 
helper.stub(:about_path).and_return('/about') 
expect(helper.css_class_active_for(about_path)).to match /active/ 
end 
it "returns 'inactive' if the current path does not match the link path" do 
helper.stub(:current_path).and_return('/') 
helper.stub(:about_path).and_return('/about') 
expect(helper.css_class_active_for(about_path)).to match /inactive/ 
end 
end 
end
CODE FOR css_class_active_for 
HELPER 
layout_helper.rb 
module LayoutHelper 
def css_class_active_for link_path 
link_path == current_path ? "active" : "inactive" 
end 
end
THAT'S IT 
Xander Miller 
twitter: @pareidoliax 
blog: http://pareidoliax.ca 
github: pareidoiax

More Related Content

What's hot

RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2Rory Gianni
 
RoR 101: Session 3
RoR 101: Session 3RoR 101: Session 3
RoR 101: Session 3Rory Gianni
 
RoR 101: Session 5
RoR 101: Session 5RoR 101: Session 5
RoR 101: Session 5Rory Gianni
 
WordCamp Geneva Presentation - Customising WordPress' Admin Panel - 19 Nov. 2016
WordCamp Geneva Presentation - Customising WordPress' Admin Panel - 19 Nov. 2016WordCamp Geneva Presentation - Customising WordPress' Admin Panel - 19 Nov. 2016
WordCamp Geneva Presentation - Customising WordPress' Admin Panel - 19 Nov. 2016Jesper van Engelen
 
Single Page Web Apps with Backbone.js and Rails
Single Page Web Apps with Backbone.js and RailsSingle Page Web Apps with Backbone.js and Rails
Single Page Web Apps with Backbone.js and RailsPrateek Dayal
 
Intro to-rails-webperf
Intro to-rails-webperfIntro to-rails-webperf
Intro to-rails-webperfNew Relic
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à RubyMicrosoft
 
RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6Rory Gianni
 
RSpec User Stories
RSpec User StoriesRSpec User Stories
RSpec User Storiesrahoulb
 
Story Driven Development With Cucumber
Story Driven Development With CucumberStory Driven Development With Cucumber
Story Driven Development With CucumberSean Cribbs
 
RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6Rory Gianni
 
Cucumber Ru09 Web
Cucumber Ru09 WebCucumber Ru09 Web
Cucumber Ru09 WebJoseph Wilk
 
Java script functions
Java script   functionsJava script   functions
Java script functionschauhankapil
 
Simplifying Code: Monster to Elegant in 5 Steps
Simplifying Code: Monster to Elegant in 5 StepsSimplifying Code: Monster to Elegant in 5 Steps
Simplifying Code: Monster to Elegant in 5 Stepstutec
 
The story became happy with itamae
The story became happy with itamaeThe story became happy with itamae
The story became happy with itamaeNobutoshi Ogata
 
Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Engine Yard
 
정오의 데이트 for iOS 코드 정리
정오의 데이트 for iOS 코드 정리정오의 데이트 for iOS 코드 정리
정오의 데이트 for iOS 코드 정리태준 김
 
You're Doing It Wrong
You're Doing It WrongYou're Doing It Wrong
You're Doing It Wrongbostonrb
 
Ruby on Rails - UNISO
Ruby on Rails - UNISORuby on Rails - UNISO
Ruby on Rails - UNISOLucas Renan
 

What's hot (20)

RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
 
RoR 101: Session 3
RoR 101: Session 3RoR 101: Session 3
RoR 101: Session 3
 
RoR 101: Session 5
RoR 101: Session 5RoR 101: Session 5
RoR 101: Session 5
 
WordCamp Geneva Presentation - Customising WordPress' Admin Panel - 19 Nov. 2016
WordCamp Geneva Presentation - Customising WordPress' Admin Panel - 19 Nov. 2016WordCamp Geneva Presentation - Customising WordPress' Admin Panel - 19 Nov. 2016
WordCamp Geneva Presentation - Customising WordPress' Admin Panel - 19 Nov. 2016
 
Single Page Web Apps with Backbone.js and Rails
Single Page Web Apps with Backbone.js and RailsSingle Page Web Apps with Backbone.js and Rails
Single Page Web Apps with Backbone.js and Rails
 
Intro to-rails-webperf
Intro to-rails-webperfIntro to-rails-webperf
Intro to-rails-webperf
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à Ruby
 
Infografia
Infografia Infografia
Infografia
 
RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6
 
RSpec User Stories
RSpec User StoriesRSpec User Stories
RSpec User Stories
 
Story Driven Development With Cucumber
Story Driven Development With CucumberStory Driven Development With Cucumber
Story Driven Development With Cucumber
 
RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6
 
Cucumber Ru09 Web
Cucumber Ru09 WebCucumber Ru09 Web
Cucumber Ru09 Web
 
Java script functions
Java script   functionsJava script   functions
Java script functions
 
Simplifying Code: Monster to Elegant in 5 Steps
Simplifying Code: Monster to Elegant in 5 StepsSimplifying Code: Monster to Elegant in 5 Steps
Simplifying Code: Monster to Elegant in 5 Steps
 
The story became happy with itamae
The story became happy with itamaeThe story became happy with itamae
The story became happy with itamae
 
Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel
 
정오의 데이트 for iOS 코드 정리
정오의 데이트 for iOS 코드 정리정오의 데이트 for iOS 코드 정리
정오의 데이트 for iOS 코드 정리
 
You're Doing It Wrong
You're Doing It WrongYou're Doing It Wrong
You're Doing It Wrong
 
Ruby on Rails - UNISO
Ruby on Rails - UNISORuby on Rails - UNISO
Ruby on Rails - UNISO
 

Viewers also liked

Data Driven Game development
Data Driven Game developmentData Driven Game development
Data Driven Game developmentKostas Anagnostou
 
Bootstrap Introduction
Bootstrap IntroductionBootstrap Introduction
Bootstrap IntroductionAndrea Tarr
 
Introduction to Bootstrap and elements of Markov Chains
Introduction to Bootstrap and elements of Markov ChainsIntroduction to Bootstrap and elements of Markov Chains
Introduction to Bootstrap and elements of Markov ChainsUniversity of Salerno
 
Bootstrap (finance)
Bootstrap (finance)Bootstrap (finance)
Bootstrap (finance)manimaddula
 
Bootstrapping Entrepeneurs & Their Business Ppt
Bootstrapping Entrepeneurs & Their Business PptBootstrapping Entrepeneurs & Their Business Ppt
Bootstrapping Entrepeneurs & Their Business PptCaroline Cummings
 
Study on The Affect of Aspect Building Form and Layout
Study on The Affect of Aspect Building Form and Layout Study on The Affect of Aspect Building Form and Layout
Study on The Affect of Aspect Building Form and Layout beta paramita
 
Building impressive layout systems with vaadin
Building impressive layout systems with vaadinBuilding impressive layout systems with vaadin
Building impressive layout systems with vaadinPeter Lehto
 
Swppp training 2015_a
Swppp training 2015_aSwppp training 2015_a
Swppp training 2015_aJason Donati
 
The A to Z of Building a Responsive SharePoint Site with Bootstrap
The A to Z of Building a Responsive SharePoint Site with BootstrapThe A to Z of Building a Responsive SharePoint Site with Bootstrap
The A to Z of Building a Responsive SharePoint Site with BootstrapThomas Daly
 

Viewers also liked (12)

Data Driven Game development
Data Driven Game developmentData Driven Game development
Data Driven Game development
 
Bootstrap Introduction
Bootstrap IntroductionBootstrap Introduction
Bootstrap Introduction
 
Introduction to Bootstrap and elements of Markov Chains
Introduction to Bootstrap and elements of Markov ChainsIntroduction to Bootstrap and elements of Markov Chains
Introduction to Bootstrap and elements of Markov Chains
 
Bootstrap (finance)
Bootstrap (finance)Bootstrap (finance)
Bootstrap (finance)
 
Bootstrapping Entrepeneurs & Their Business Ppt
Bootstrapping Entrepeneurs & Their Business PptBootstrapping Entrepeneurs & Their Business Ppt
Bootstrapping Entrepeneurs & Their Business Ppt
 
Study on The Affect of Aspect Building Form and Layout
Study on The Affect of Aspect Building Form and Layout Study on The Affect of Aspect Building Form and Layout
Study on The Affect of Aspect Building Form and Layout
 
Building impressive layout systems with vaadin
Building impressive layout systems with vaadinBuilding impressive layout systems with vaadin
Building impressive layout systems with vaadin
 
Swppp training 2015_a
Swppp training 2015_aSwppp training 2015_a
Swppp training 2015_a
 
The estimating process
The estimating processThe estimating process
The estimating process
 
The A to Z of Building a Responsive SharePoint Site with Bootstrap
The A to Z of Building a Responsive SharePoint Site with BootstrapThe A to Z of Building a Responsive SharePoint Site with Bootstrap
The A to Z of Building a Responsive SharePoint Site with Bootstrap
 
Estimation
EstimationEstimation
Estimation
 
Types of estimates
Types of estimatesTypes of estimates
Types of estimates
 

Similar to Building Dynamic Navigation in your Rails 4 Layout

FamilySearch Reference Client
FamilySearch Reference ClientFamilySearch Reference Client
FamilySearch Reference ClientDallan Quass
 
Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Joao Lucas Santana
 
Ruby on Rails na Unip
Ruby on Rails na UnipRuby on Rails na Unip
Ruby on Rails na UnipFabio Akita
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overviewYehuda Katz
 
Working with Javascript in Rails
Working with Javascript in RailsWorking with Javascript in Rails
Working with Javascript in RailsSeungkyun Nam
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the FinishYehuda Katz
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Chris Alfano
 
How to develop modern web application framework
How to develop modern web application frameworkHow to develop modern web application framework
How to develop modern web application frameworktechmemo
 
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldEfficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldMarakana Inc.
 
Rails 6 frontend frameworks
Rails 6 frontend frameworksRails 6 frontend frameworks
Rails 6 frontend frameworksEric Guo
 
Fisl 11 - Dicas de Desenvolvimento Web com Ruby
Fisl 11 - Dicas de Desenvolvimento Web com RubyFisl 11 - Dicas de Desenvolvimento Web com Ruby
Fisl 11 - Dicas de Desenvolvimento Web com RubyFabio Akita
 
Twitter bootstrap on rails
Twitter bootstrap on railsTwitter bootstrap on rails
Twitter bootstrap on railsMasakuni Kato
 
Angular Tutorial Freshers and Experienced
Angular Tutorial Freshers and ExperiencedAngular Tutorial Freshers and Experienced
Angular Tutorial Freshers and Experiencedrajkamaltibacademy
 
Aplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com BackboneAplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com BackboneRafael Felix da Silva
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium AppsNate Abele
 
AnkaraJUG Kasım 2012 - PrimeFaces
AnkaraJUG Kasım 2012 - PrimeFacesAnkaraJUG Kasım 2012 - PrimeFaces
AnkaraJUG Kasım 2012 - PrimeFacesAnkara JUG
 

Similar to Building Dynamic Navigation in your Rails 4 Layout (20)

FamilySearch Reference Client
FamilySearch Reference ClientFamilySearch Reference Client
FamilySearch Reference Client
 
Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)
 
Ruby on Rails na Unip
Ruby on Rails na UnipRuby on Rails na Unip
Ruby on Rails na Unip
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Working with Javascript in Rails
Working with Javascript in RailsWorking with Javascript in Rails
Working with Javascript in Rails
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
The Rails Way
The Rails WayThe Rails Way
The Rails Way
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011
 
How to develop modern web application framework
How to develop modern web application frameworkHow to develop modern web application framework
How to develop modern web application framework
 
EVOLVE'14 | Enhance | Gabriel Walt | Sightly Component Development
EVOLVE'14 | Enhance | Gabriel Walt | Sightly Component DevelopmentEVOLVE'14 | Enhance | Gabriel Walt | Sightly Component Development
EVOLVE'14 | Enhance | Gabriel Walt | Sightly Component Development
 
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldEfficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
 
JS-05-Handlebars.ppt
JS-05-Handlebars.pptJS-05-Handlebars.ppt
JS-05-Handlebars.ppt
 
Rails 6 frontend frameworks
Rails 6 frontend frameworksRails 6 frontend frameworks
Rails 6 frontend frameworks
 
Fisl 11 - Dicas de Desenvolvimento Web com Ruby
Fisl 11 - Dicas de Desenvolvimento Web com RubyFisl 11 - Dicas de Desenvolvimento Web com Ruby
Fisl 11 - Dicas de Desenvolvimento Web com Ruby
 
Twitter bootstrap on rails
Twitter bootstrap on railsTwitter bootstrap on rails
Twitter bootstrap on rails
 
Angular Tutorial Freshers and Experienced
Angular Tutorial Freshers and ExperiencedAngular Tutorial Freshers and Experienced
Angular Tutorial Freshers and Experienced
 
Aplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com BackboneAplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com Backbone
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium Apps
 
AnkaraJUG Kasım 2012 - PrimeFaces
AnkaraJUG Kasım 2012 - PrimeFacesAnkaraJUG Kasım 2012 - PrimeFaces
AnkaraJUG Kasım 2012 - PrimeFaces
 
Play vs Rails
Play vs RailsPlay vs Rails
Play vs Rails
 

Recently uploaded

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

Building Dynamic Navigation in your Rails 4 Layout

  • 3. STATIC SITE NAVIGATION WITH BOOTSTRAP
  • 4. BOOTSTRAP SOURCE <nav> <ul class="nav nav-pills"> <li class="inactive"><a href="/">Home</a></li> <li class="active"><a href="/portfolio">Portfolio</a></li> <li class="inactive"><a href="/about">About</a></li> <li class="inactive"><a href="/contact">Contact</a></li> </ul> </nav>
  • 6. REQUIREMENTS We need to be able to check if we are rendering the destination of the link and we then need to be able to set the class accordingly.
  • 7. REQUIREMENTS rake routes Prefix Verb URI Pattern Controller#Action portfolio GET /portfolio(.:format) welcome#portfolio about GET /about(.:format) welcome#about contact GET /contact(.:format) welcome#contact shopify GET /shopify(.:format) welcome#shopify root GET / welcome#home link_to("About", about_path)
  • 8. REQUIREMENTS ▸ need to determine current_path ▸ need to compare it to the link_path ▸ neet to set css_class_active_for(link_path) <%= content_tag :li, link_to("About", about_path), class: "#{css_class_active_for(about_path)}" %>
  • 9. TARGET CODE _navbar.html.erb <nav> <ul class="nav nav-pills"> <%= content_tag :li, link_to("Home", root_path), class: "#{css_class_active_for(root_path)}" %> <%= content_tag :li, link_to("Portfolio", portfolio_path), class: "#{css_class_active_for(portfolio_path)}" %> <%= content_tag :li, link_to("About", about_path), class: "#{css_class_active_for(about_path)}" %> <%= content_tag :li, link_to("Contact", contact_path), class: "#{css_class_active_for(contact_path)}" %> </ul> </nav>
  • 12. TEST FOR current_path HELPER application_helper_spec.rb # helper is an instance of ActionView::Base configured with the # ApplicationHelper and all of Rails' built-in helpers require "spec_helper" describe ApplicationHelper do context '#current_path' do it "returns #request.env['PATH_INFO']" do helper.request.env['PATH_INFO'] = '/welcome' helper.current_path.should eq('/welcome') end end end
  • 13. CODE FOR current_path HELPER application_helper.rb module ApplicationHelper def current_path request.env['PATH_INFO'] end end
  • 14. TEST OF css_class_active_for HELPER layout_helper_spec.rb require "spec_helper" describe LayoutHelper do context "#css_class_active_for" do it "returns 'active' if the current path matches link path" do helper.stub(:current_path).and_return('/about') helper.stub(:about_path).and_return('/about') expect(helper.css_class_active_for(about_path)).to match /active/ end it "returns 'inactive' if the current path does not match the link path" do helper.stub(:current_path).and_return('/') helper.stub(:about_path).and_return('/about') expect(helper.css_class_active_for(about_path)).to match /inactive/ end end end
  • 15. require "spec_helper" describe LayoutHelper do context "#css_class_active_for" do it "returns 'active' if the current path matches link path" do helper.stub(:current_path).and_return('/about') helper.stub(:about_path).and_return('/about') expect(helper.css_class_active_for(about_path)).to match /active/ end it "returns 'inactive' if the current path does not match the link path" do helper.stub(:current_path).and_return('/') helper.stub(:about_path).and_return('/about') expect(helper.css_class_active_for(about_path)).to match /inactive/ end end end
  • 16. CODE FOR css_class_active_for HELPER layout_helper.rb module LayoutHelper def css_class_active_for link_path link_path == current_path ? "active" : "inactive" end end
  • 17. THAT'S IT Xander Miller twitter: @pareidoliax blog: http://pareidoliax.ca github: pareidoiax