Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6880165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:56:22+00:00 2026-05-27T04:56:22+00:00

I have a simple nav bar like so: <ul class=nav> <li><a href=<%= index_url %>>Home</a></li>

  • 0

I have a simple nav bar like so:

<ul class="nav">
   <li><a href="<%= index_url %>">Home</a></li>
   <li><a href="#about">About</a></li>
   <li><a href="<%= users_url %>">Users</a></li>
</ul>

Which is part of the application.html.erb file in a rails project. I would like the class="active" attribute to be set on whatever the current page is, however this is complicated since the nav bar is in the template that yields to the rest of the app with <%= yield %>. How can I reliably set the active tab based on what page I’m loading?

…javascript removed because it was distracting from the actual question.

To clarify:

Rails loads the application.html.erb which renders everything up to and including this nav before calling <%= yield %> and rendering whatever view I’m loading.

I want the “Users” tab to have the class="active" when I’m displaying the users/index.html.erb file, or the users/show.html.erb (basically anything from the user model) is there a proper rails way to do this?

I know I could move the nav bar to each model’s view template and manually set the active class, but that’s not very “DRY”.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:56:23+00:00Added an answer on May 27, 2026 at 4:56 am

    Probably not exactly what you’re looking for, but I typically just write a quick helper named “tab” that lets me set a variable into my view for the tab, and then use conditionals to set up the class on the tabs.

    # Helper
    def tab(value)
      @page_tab = value
    end
    
    # View
    <% tab "about" %>
    
    # Layout    
    <ul class="nav">
       <li><a href="<%= index_url %>" <%= class=\"active\" if @page_tab == "home" %>>Home</a></li>
       <li><a href="#about" <%= class=\"active\" if @page_tab == "about" %>>About</a></li>
       <li><a href="<%= users_url %>" <%= class=\"active\" if @page_tab == "users" %>>Users</a></li>
    </ul>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple app with a nav bar controller and I would like
I have a simple nav made up of the following HTML; <div class=inner> <div
I have simple class with width and height member fields which define number of
I have a simple grouped uitableview with a nav bar and an Edit nav
Using twitter bootstrap (2), I have a simple page with a nav bar, and
My home page is largely the same for all users, which makes it a
I have a link being generated that looks like so: <a target=_blank title=Test href=file:///c:/test.xls>Test</a>
I have a very simple HTML layout where a vertical navigation bar should sit
I have a simple navigation bar at the top of the page with several
This simple HTML5 layout is intended to have a navigation bar on the left

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.