# Help with CSS in application.html.erb

**URL:** <https://www.ruby-forum.com/t/help-with-css-in-application-html-erb/132816>\
**Category:** Rails\
**Created:** [March 25, 2008, 12:37am UTC](https://www.ruby-forum.com/t/help-with-css-in-application-html-erb/132816 "2008-03-25T00:37:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 25, 2008, 12:37am UTC](https://www.ruby-forum.com/t/help-with-css-in-application-html-erb/132816/1 "2008-03-25T00:37:54Z")

</div>

I have this code in application.html.erb:

- \<%= link\_to \_(' **Repair tickets**'), repair\_tickets\_path%\> 
- \<%= link\_to \_(' **Incidences**'), incidences\_path%\>
- \<%= link\_to \_(' **Tickets**'), tickets\_path%\>
- \<%= link\_to \_(' **Clients**'), clients\_path%\>
- \<%= link\_to \_(' **Products**'), products\_path %\>

I want that once I have pressed the Incidences link, the li class  
becomes

- , meanwhile the li class from Repair  
tickets becomes 
- . How can I do that?

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 25, 2008, 1:01am UTC](https://www.ruby-forum.com/t/help-with-css-in-application-html-erb/132816/2 "2008-03-25T01:01:00Z")

</div>

John S. wrote:

> I have this code in application.html.erb:
> 
> - \<%= link\_to \_(' **Repair tickets**'), repair\_tickets\_path%\> 
> - \<%= link\_to \_(' **Incidences**'), incidences\_path%\>
> - \<%= link\_to \_(' **Tickets**'), tickets\_path%\>
> - \<%= link\_to \_(' **Clients**'), clients\_path%\>
> - \<%= link\_to \_(' **Products**'), products\_path %\>
> 
> I want that once I have pressed the Incidences link, the li class  
> becomes
> 
> - , meanwhile the li class from Repair  
> tickets becomes 
> - . How can I do that?

I’m going to assume you’re using Prototype. If not, I’m sure there is a  
JQuery equivalent.

Look up addClassName and removeClassName. You will need to create an  
onclick event handler. I’d probably give each LI an id and pass that  
into a function, but you could do it in the inline onclick handler if  
you really wanted to. It certainly wouldn’t be DRY.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 25, 2008, 1:09am UTC](https://www.ruby-forum.com/t/help-with-css-in-application-html-erb/132816/3 "2008-03-25T01:09:04Z")

</div>

The way i’ve always done this is to set a global variable to some  
value, and check for this in the view. It is messy though so i’m  
interested to hear any better solutions.

example of what i do:

== controller ==

def some\_action  
@active\_menu\_item = “products”  
end

== view ==

- ... 

You could clean this up my making a helper method for the view, and a  
before filter in your products controller that sets the active menu  
item, but this is the only solution I’ve managed to find.

On Mar 25, 12:37 pm, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 25, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/help-with-css-in-application-html-erb/132816/4 "2008-03-25T17:33:59Z")

</div>

I wonder if link\_to\_unless\_current might work elegantly for you:

[http://api.rubyonrails.com/classes/ActionView/Helpers/UrlHelper.html#M000915](http://api.rubyonrails.com/classes/ActionView/Helpers/UrlHelper.html#M000915)

You could use CSS to make the plain text in your list correspond to  
your current “current” style.

-Kyle

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 25, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/help-with-css-in-application-html-erb/132816/5 "2008-03-25T20:20:42Z")

</div>

Hey that’s cool! You also just made me realise that link\_to\_unless  
would work quite nicely in different situations as well! 🙂

On Wed, Mar 26, 2008 at 5:33 AM, Kyle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > def some\_action  
> > before filter in your products controller that sets the active menu
> > 
> > > ```
> > > <li><%= link_to _('<b>Tickets</b>'), tickets_path%></li>
> > > 
> > > ```

–  
Jonathan
