aboutsummaryrefslogtreecommitdiff
path: root/content/post/2008-9-27-highlight-link-based-on-current-page-in-rails.markdown
diff options
context:
space:
mode:
authorJulio Capote <jcapote@gmail.com>2018-11-06 03:03:41 +0000
committerJulio Capote <jcapote@gmail.com>2018-11-06 03:03:41 +0000
commit4b489a049a0063bbb1fd9f0c0f74ce1ee9f87a86 (patch)
tree98af5707e30150af482e297bed9cd4e9b5477e6a /content/post/2008-9-27-highlight-link-based-on-current-page-in-rails.markdown
parenta62a3e7755579d93ce3a87243dd277575930fffe (diff)
downloadcapotej.com-4b489a049a0063bbb1fd9f0c0f74ce1ee9f87a86.tar.gz
import old posts
Diffstat (limited to 'content/post/2008-9-27-highlight-link-based-on-current-page-in-rails.markdown')
-rw-r--r--content/post/2008-9-27-highlight-link-based-on-current-page-in-rails.markdown31
1 files changed, 31 insertions, 0 deletions
diff --git a/content/post/2008-9-27-highlight-link-based-on-current-page-in-rails.markdown b/content/post/2008-9-27-highlight-link-based-on-current-page-in-rails.markdown
new file mode 100644
index 0000000..e696208
--- /dev/null
+++ b/content/post/2008-9-27-highlight-link-based-on-current-page-in-rails.markdown
@@ -0,0 +1,31 @@
+---
+layout: post
+title: "Highlight link based on current page in rails"
+date: 2008-09-27T19:47:00Z
+comments: false
+permalink: /post/52081481/highlight-link-based-on-current-page-in-rails
+categories:
+---
+
+
+
+This is common pattern in website navigation, where it highlights the link (usually by setting `class=”active”`) that took you to the current page while you are on that page.
+
+First, define a helper:
+
+```ruby
+def is_active?(page_name)
+ "active" if params[:action] == page_name
+end
+
+```
+
+Then call it in your link_to’s in your layout as such:
+
+```ruby
+link_to 'Home', '/', :class => is_active?("index")
+link_to 'About', '/about', :class => is_active?("about")
+link_to 'contact', '/contact', :class => is_active?("contact")
+```
+
+This effect is achieved due to how link_to handles being passed `nil` for its `:class`, so when `is_active?` returns `nil` (because its not the current page), `link_to` outputs nothing as its class (not `class=””` as you might expect).