Implementing a dynamic menu feature using Ruby on Rails

I have created a secondary navigation bar to help users easily navigate through different sections of their account.

DASHBOARD | ACTIVITY | PORTFOLIO | PROJECTS

My goal is to make the current page stand out by making its CSS bold. For example, when a user is on the 'dashboard' page, I want the navigation bar to appear like this:

DASHBOARD | ACTIVITY | PORTFOLIO | PROJECTS

I have tried using code like:

if current_page?(controller: 'users', action: 'edit)
  <span class="bold">DASHBOARD</span>
else
  DASHBOARD
end

However, this code is not working as expected due to the nested partials I am using to generate the views.

For instance, each user can access their own account information, but since they may have multiple projects, I render a call to a partial located in

views/projects/_list_by_owner.html.erb
and pass in

@projects = Project.where(:owner_id => current_user.id)
from the users_controller#show.

Because the view is being generated from the users_controller rather than the projects_controller, it is not recognizing the correct current_page() value.

Do you have any suggestions on how to further explore the controller/action nesting or perhaps a more efficient way to dynamically generate the CSS class?

Answer №1

Here's my approach

Helper function

def highlight_current_path(path)
  "active" if request.url.include?(path)
end

Implement in View

<%= link_to "users", posts_path, class: highlight_current_path(posts_path) %>

UPDATE: Providing context

If you are working on a template and need to generate complex HTML elements or define classes, using helper functions can be useful. It allows you to modify the appearance of content without accessing the database.

To incorporate this functionality, navigate to App >> helpers >> users_helper.rb

Add the following code:

module LocationsHelper
    def highlight_current_path(path)
        "active" if request.url.include?(path)
    end
end

This function will detect when highlight_current_path is invoked and apply the 'active' class to the HTML element if it matches the current path.

You can then style the 'active' class using simple CSS

a.active{
  font-weight:700;
  text-decoration:underline;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Identifying a change in the source location of an iframe element

I am working with an iframe object that is currently set to a specific page's URL. Here is an example: <iframe src="http://en.wikipedia.org/wiki/Special:Random"></iframe> My goal is to display an alert whenever the location of the iframe ...

Switching the color of NavLink text when onclick occurs

Is there a way to modify the text color of the links within the navbar using React? navLink1{ color: black; } <div className="left-navlinks"> <img className="logo" src={logo}/> &l ...

Is it possible to populate an input field without first having to select it in HTML or JavaScript?

Is there a way to populate an input field with JavaScript without requiring the user to manually click on it? I want the user to simply type their answer and have it immediately appear in the input field. Here is the HTML code for reference: <input id= ...

React-Accessible-Accordion is not displaying the toggle icon

I have integrated React-accessible-accordion into my React application. The click functionality is working correctly, but I am facing an issue with the arrow icon not appearing on the accordion. Upon comparing my DOM structure with the example provided in ...

Struggling to apply size in percentage to several images used as a background

Displaying two background images with different positioning: html { background: top left no-repeat url(http://wallpapers.pupazzo.org/animals/Peek-a-Boo_Red_Fox_Kit.jpg), top right no-repeat url(http://www.dcwild.com/images/Fox-Kit-Rive ...

Converting CSS colors from RGBA to hexadecimal format: A step-by-step guide

Within my selenium code, I am faced with the challenge of verifying that the background color code is #192856. However, when obtaining the CSS property of the element, it returns the color in rgba format. How can I retrieve the values in hex format? quick ...

How can I get my HTML DIVs to automatically move downward instead of upwards?

I'm currently developing a chat script that includes the feature to minimize specific chats, but I've encountered an issue with making the header of the div push down. Despite researching extensively, I haven't been able to find a solution t ...

Display and animate a div when hovered over inside a wrapper element

I have a unique challenge with an image gallery that I'm working on. Each image is enclosed within a box, and I am looking to incorporate hidden icons on the right and left sides of the box. These icons should not be visible to the user, but rather ap ...

Words with emphasized border and drop shadow effect

I am trying to achieve a specific font style. If it's not possible, I would like to get as close as possible to the desired style. However, due to IE support requirements, I am unable to use text-stroke. https://i.sstatic.net/JwQyb.png The closest a ...

Using jQuery to alter the attributes of a hover class

Is there a way to modify the Hover-Background-Color? <style> .myhoverclass {background:#FFF;} </style> I am using this code to toggle: $('.box a').hover( function(){ $(this).toggleClass('myhoverclass&apo ...

How CSS can affect the layout of elements on a webpage

I am looking to achieve something similar to this: The left box should maintain a fixed width, while the right one resizes to fit the full width of the browser window. I also need the height of both boxes to be resizable. Apologies for any inconvenience ...

jQuery is not active on responsive designs

I have implemented a script in my JavaScript code that changes the color of the navigation bar when scrolling. The navigation bar transitions to a white color as you scroll down. However, I am facing an issue with responsiveness and would like to deactivat ...

The preferred size variable for the allocation allotment is malfunctioning

I am working on creating a draggable split panel for an editor, similar to the behavior of the Console panel in CodeSandbox: Clicking on Console expands the panel and changes the arrow to ArrowDown to close it. The border of the panel is draggable. If you ...

What is the best way to ensure a textarea element fills the entire height using Bootstrap 4?

Is there a way to ensure the textarea element dynamically adjusts its height to fill the space between the header and footer without causing a scroll bar when using bootstrap 4? Here is the HTML code snippet I've been working with: #container { ...

`Trying to conceal the tr elements on button click proves tricky with jquery`

I have a pair of buttons, #btn1 and #btn2, as well as two table rows, #tr1 and #tr2. The goal is to toggle the active state of the buttons when they are clicked by the user. The specific requirements are: When the button is set to active, the elements ...

Reduce / condense text that is overflowing

Take a look at the image I've attached. The text is overflowing outside the div, and I want to collapse it with a "Read more" link at the end. Users should be able to click on the "Read more" link to expand the text. I'm currently working on a p ...

@SpringBootApplication is unable to retrieve static files

Located within the directory path: src/main/resources/static/ js css img etc.. Despite numerous attempts to configure addResourceHandlers, the static files remain undetected. The issue persists no matter what I try. ...

Validation failure in the CSS document - Syntax error

I have searched high and low for a solution to my CSS 3.0 validation issue, but none seem to fit the bill. I am struggling with parse errors in the beginning and despite checking for invisible characters, I can't seem to pinpoint the problem. What co ...

I am experiencing issues with the interpolation of my SASS variables when they are placed into

I am relatively new to the Nuxt ecosystem and I must say it's an awesome package that really simplifies our lives. Currently, I am attempting to incorporate sass into my project. Despite following the steps outlined in the documentation, my build is ...

Adjust the positioning of the share section to take up the entire width on mobile devices, while displaying differently

When attempting to position my share div to occupy the full width of one of its sibling (person) elements, I encountered some issues. The share div is designed to only display when a button is clicked. You can view the live version and see the problem by ...