Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Michael Lee 🍕
Michael Lee 🍕

Posted on

     

Determine the current route in Rails

Let's say you're building a sweet navigation in your Rails application and the design you're implementing shows that you've got different states that have different visual cues.

Those states might be inactive, hovering and active or current. Inactive and hovering seems pretty straight forward, you can do all that in CSS.

.Nav-Item {  // Styles for the "inactive" nav item state  // would go here.}.Nav-Item:hover {  // Styles for the "hover" nav item state  // would go here.}
Enter fullscreen modeExit fullscreen mode

But the active or nav item state that indicates you're currently on the same page as an item in the nav isn't a simple CSS solution. To solve this, you'll need to use a built-in Rails helper calledcurrent_page?.

With thecurrent_page? helper you can pass it things like a relative or absolute path that you'd like to check or you can even pass it actions and controllers with parameters for it to check. Doing so will return either atrue orfalse.

How this could look in your mark up is this,

<%= link_to({ controller: 'page', action: 'about' }, { class: "Nav-Item #{'is-current-page' if current_page?(controller: 'page', action: 'about')}" }) do %>  About Us<% end %>
Enter fullscreen modeExit fullscreen mode

I'm using thecurrent_page? helper inside alink_to helper block. Specifically it is being passed to the class attribute where it is saying, if the current pagecurrent_page? is the same as the page handled by theabout action in thepage controller, then add the value ofis-current-page to the class attribute.

Now I could have this in my CSS,

.Nav-Item {  // Styles for the "inactive" nav item state  // would go here.}.Nav-Item:hover {  // Styles for the "hover" nav item state  // would go here.}.Nav-Item.is-current-page {  // Styles for the nav item that corresponds  // with the current page the user is viewing}
Enter fullscreen modeExit fullscreen mode

Now we're able to handle inactive, hovering and current page states in a Rails application nav.


Originallyposted on michaelsoolee.com.

Thanks for taking the time to read this article! I'd love to stay in contact and send you tips on programming and design and making side projects through my newsletter.Click here to sign up.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
jsrn profile image
James
web developer, rubyist, friend
  • Email
  • Location
    Birmingham, United Kingdom
  • Work
    Senior Developer
  • Joined

This is handy, thank you!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Maker of things, giver of high-fives 🖐
  • Location
    Cary, NC
  • Education
    NC State University
  • Work
    Director of Engineering
  • Joined

More fromMichael Lee 🍕

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp