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

  • SEARCH
  • Home
  • 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 6371207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:01:58+00:00 2026-05-25T01:01:58+00:00

My question is very similar to this one : Changing Current Tab in Rails

  • 0

My question is very similar to this one : Changing Current Tab in Rails

I am trying to add a ‘selected’ class to my link regarding the id of my portfolios controller’s show action

Here is what I’ve tried:

<ul>
  <% for portfolio in @portfolios %>
    <li class="<%= controller.class == PortfoliosController and controller.action_name == 'show' and controller.params[:id] == portfolio.id ? 'selected' : '' %>"><%= link_to portfolio.name, portfolio %></li>
  <% end %>
</ul>

But it seems that the following:

controller.params[:id] == portfolio.id

doesn’t match correctly and I don’t understand why..

Thanks for your help!

  • 1 1 Answer
  • 0 Views
  • 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-25T01:01:58+00:00Added an answer on May 25, 2026 at 1:01 am

    You might be comparing string and numerical values, which in Ruby are not considered equivalent. A conversion of one of or the other might help. In fact, if you roll this up in a helper method, it might make it a lot easier to follow:

    def class_for_porfolio_entry(portfolio)
      controller.class == PortfoliosController and
        controller.action_name == 'show' and
        controller.params[:id].to_i == portfolio.id ?
          'selected' :
          ''
    end
    

    You’ve got a whole lot going on there, so you might want to look at ways of reducing the complexity, for instance using params instead:

    def class_for_porfolio_entry(portfolio)
      params[:controller] == 'portfolios' and
        params[:action] == 'show' and
        params[:id].to_i == portfolio.id ?
          'selected' :
          ''
    end
    

    This could be simplified further if you had a boolean flag set in your controller that is later used as required, avoiding hard-coding something like this:

    def class_for_porfolio_entry(portfolio)
      if (@show_selected_portfolio and @portfolio.id == portfolio.id)
        'selected'
      else
        ''
      end
    end
    

    This presumes you have an instance variable @portfolio, which is usually the case in any controller’s show method, and that you will set @show_selected_portfolio to true in any controller method where this logic applies.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question popped up a very similar question, this one . But the accepted
This is very similar to another recent question: How can I return the current
My current problem is very similar to this one . I have a downloadFile(URL)
I have an error very similar to the one addressed in this question .
I'm asking a question very similar to this one —dare I say identical? An
My question is very similar to this one . The application I'm developing is
this oughta be an easy one. My question is very similar to this one
I have a question that is very similar to this one: Setting focus to
Firstly, I realise that this is a very similar question to this one: Which
My question is very similar to this one How to reference a custom field

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.