# How to add a class to my anchor?

**URL:** <https://www.ruby-forum.com/t/how-to-add-a-class-to-my-anchor/147673>\
**Category:** Rails\
**Created:** [September 4, 2008, 5:40am UTC](https://www.ruby-forum.com/t/how-to-add-a-class-to-my-anchor/147673 "2008-09-04T05:40:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 4, 2008, 5:40am UTC](https://www.ruby-forum.com/t/how-to-add-a-class-to-my-anchor/147673/1 "2008-09-04T05:40:35Z")

</div>

Hi,

I have

\<%= link\_to ‘Add a New Prescription’, :controller =\> ‘forms’, :action  
=\> ‘new’, :class =\> “sidebar2Text” %\>

which is producing

[Refill a  
Prescription](https://www.ruby-forum.com/easyrx/public/order/new?class=sidebar2Text)

How do I make the “class” its own attribute?

Thanks, - Dave

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 4, 2008, 7:13am UTC](https://www.ruby-forum.com/t/how-to-add-a-class-to-my-anchor/147673/2 "2008-09-04T07:13:04Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hi,
> 
> I have
> 
> \<%= link\_to ‘Add a New Prescription’, :controller =\> ‘forms’, :action  
> =\> ‘new’, :class =\> “sidebar2Text” %\>
> 
> which is producing
> 
> [Refill a  
> Prescription](https://www.ruby-forum.com/easyrx/public/order/new?class=sidebar2Text)
> 
> How do I make the “class” its own attribute?
> 
> Thanks, - Dave

Dave,

Since the options hash can include any extra variables as query  
parameters, you need to make sure that link\_to realizes you’ve moved  
from the options hash to the html\_options hash… you just need to wrap  
the regular options in {} like this:

\<%= link\_to ‘Add a New Prescription’, {:controller =\> ‘forms’, :action  
=\> ‘new’}, :cass =\> ‘sidebar2Text’ %\>

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 4, 2008, 9:28am UTC](https://www.ruby-forum.com/t/how-to-add-a-class-to-my-anchor/147673/3 "2008-09-04T09:28:43Z")

</div>

Try this:

\<%= link\_to ‘Add a New Prescription’, {:controller =\> ‘forms’,  
:action=\> ‘new’}, :class =\> “sidebar2Text” %\>

The link\_to method receives 3 parameters, with your call, you were  
providing just two (the hash was counting as the 2nd one), the 3th  
parameter is the options hash, were you should put the HTML class.

On Thu, Sep 4, 2008 at 12:39 AM, removed\_email\_address@domain.invalid  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [Refill a  
> Prescription](https://www.ruby-forum.com/easyrx/public/order/new?class=sidebar2Text)
> 
> How do I make the “class” its own attribute?
> 
> Thanks, - Dave

–  
Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)  
João Pessoa, PB, +55 83 8867-7208
