SlideShare a Scribd company logo
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

PDF
Be happy with Ruby on Rails - CEUNSP Itu
PDF
Rails OO views
PPTX
Ch2(working with forms)
PPTX
Ruby on Rails + AngularJS + Twitter Bootstrap
PDF
Bullet: The Functional PHP Micro-Framework
PDF
Outside-in Development with Cucumber and Rspec
PDF
JAVASCRIPT NÃO-OBSTRUTIVO com jQuery
PDF
前后端mvc经验 - webrebuild 2011 session
Be happy with Ruby on Rails - CEUNSP Itu
Rails OO views
Ch2(working with forms)
Ruby on Rails + AngularJS + Twitter Bootstrap
Bullet: The Functional PHP Micro-Framework
Outside-in Development with Cucumber and Rspec
JAVASCRIPT NÃO-OBSTRUTIVO com jQuery
前后端mvc经验 - webrebuild 2011 session

What's hot (20)

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

Viewers also liked (12)

PPT
Data Driven Game development
PPTX
Bootstrap Introduction
PDF
Introduction to Bootstrap and elements of Markov Chains
PPTX
Bootstrap (finance)
PPT
Bootstrapping Entrepeneurs & Their Business Ppt
PPTX
Study on The Affect of Aspect Building Form and Layout
PDF
Building impressive layout systems with vaadin
PDF
Swppp training 2015_a
PPTX
The estimating process
PPTX
The A to Z of Building a Responsive SharePoint Site with Bootstrap
PPT
Estimation
PPTX
Types of estimates
Data Driven Game development
Bootstrap Introduction
Introduction to Bootstrap and elements of Markov Chains
Bootstrap (finance)
Bootstrapping Entrepeneurs & Their Business Ppt
Study on The Affect of Aspect Building Form and Layout
Building impressive layout systems with vaadin
Swppp training 2015_a
The estimating process
The A to Z of Building a Responsive SharePoint Site with Bootstrap
Estimation
Types of estimates
Ad

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

PDF
Survey of Front End Topics in Rails
PDF
Advanced RESTful Rails
PDF
Advanced RESTful Rails
PDF
Ruby on Rails - Introduction
PPS
Actionview
PDF
Ecossistema Rails Campus Party 09
PPTX
Catalog display
KEY
Rails 3 - The Developers Conference - 21aug2010
PDF
Rails 4.0
PDF
The Rails Way
PDF
Rupicon 2014 Action pack
PDF
Dicas de palestra
PDF
Building Large Web Applications That Are Easy to Maintain
PDF
Twitter bootstrap on rails
KEY
Introduction to Rails
PDF
Curso rails
PDF
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
PDF
Rails 3 Beautiful Code
PDF
RubyOnRails-Cheatsheet-BlaineKendall
PDF
RubyOnRails-Cheatsheet-BlaineKendall
Survey of Front End Topics in Rails
Advanced RESTful Rails
Advanced RESTful Rails
Ruby on Rails - Introduction
Actionview
Ecossistema Rails Campus Party 09
Catalog display
Rails 3 - The Developers Conference - 21aug2010
Rails 4.0
The Rails Way
Rupicon 2014 Action pack
Dicas de palestra
Building Large Web Applications That Are Easy to Maintain
Twitter bootstrap on rails
Introduction to Rails
Curso rails
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
Rails 3 Beautiful Code
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendall

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Cloud computing and distributed systems.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
cuic standard and advanced reporting.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Empathic Computing: Creating Shared Understanding
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Machine learning based COVID-19 study performance prediction
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Approach and Philosophy of On baking technology
Encapsulation_ Review paper, used for researhc scholars
Cloud computing and distributed systems.
Advanced methodologies resolving dimensionality complications for autism neur...
cuic standard and advanced reporting.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Mobile App Security Testing_ A Comprehensive Guide.pdf
A Presentation on Artificial Intelligence
Reach Out and Touch Someone: Haptics and Empathic Computing
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
sap open course for s4hana steps from ECC to s4
Empathic Computing: Creating Shared Understanding
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx
A comparative analysis of optical character recognition models for extracting...
Review of recent advances in non-invasive hemoglobin estimation
Machine learning based COVID-19 study performance prediction
gpt5_lecture_notes_comprehensive_20250812015547.pdf

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