# Setting classes and ids

**URL:** <https://www.ruby-forum.com/t/setting-classes-and-ids/138918>\
**Category:** Rails\
**Created:** [May 28, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/setting-classes-and-ids/138918 "2008-05-28T23:45:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [May 28, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/setting-classes-and-ids/138918/1 "2008-05-28T23:45:47Z")

</div>

is there a way to set the classes and ids of html elements made via  
helpers. for instance i want to create a link that results in the  
following html:

Home

is there a way to do it, or will i have to do it the old html way?

---

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [May 28, 2008, 11:48pm UTC](https://www.ruby-forum.com/t/setting-classes-and-ids/138918/2 "2008-05-28T23:48:37Z")

</div>

> is there a way to set the classes and ids of html elements made via  
> helpers. for instance i want to create a link that results in the  
> following html:
> 
> Home
> 
> is there a way to do it, or will i have to do it the old html way?

\<%= link\_to ‘Home’, ‘index.html’, :id =\> ‘current’, :class =\>  
‘northcrest’  
%\>

But I doubt you really want to link to ‘index.html’ in a rails app…  
maybe ‘/’ or home\_url or something like that.

-philip

---

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [May 29, 2008, 12:00am UTC](https://www.ruby-forum.com/t/setting-classes-and-ids/138918/3 "2008-05-29T00:00:46Z")

</div>

hey thanks for the reply. i tried what you suggested. here is the  
code:

\<%= link\_to “Results”, :action =\> “index”, :id =\> “current”, :class =\>  
“northcrest” %\>

and here is the resulting html:

[Results](https://www.ruby-forum.com/plugincompare/index/current?class=northcrest)

my first post was a little off. this is what i’m looking for as a  
result. same concept:

\<a href=“/plugincompare/index” id=“current”  
class=northcrest"\>Results

---

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [May 29, 2008, 12:13am UTC](https://www.ruby-forum.com/t/setting-classes-and-ids/138918/4 "2008-05-29T00:13:26Z")

</div>

browner than u wrote:

> hey thanks for the reply. i tried what you suggested. here is the  
> code:
> 
> \<%= link\_to “Results”, :action =\> “index”, :id =\> “current”, :class =\>  
> “northcrest” %\>
> 
> and here is the resulting html:
> 
> [Results](https://www.ruby-forum.com/plugincompare/index/current?class=northcrest)
> 
> my first post was a little off. this is what i’m looking for as a  
> result. same concept:
> 
> \<a href=“/plugincompare/index” id=“current”  
> class=northcrest"\>Results

Check out the docs for link\_to

[http://caboo.se/doc/classes/ActionView/Helpers/UrlHelper.html#M006132](http://caboo.se/doc/classes/ActionView/Helpers/UrlHelper.html#M006132)

Mainly:

link\_to(name, options = {}, html\_options = nil)

Now in this littl method, name is what appears for you to click on,  
options is actually an object which can generate a url, and html\_options  
is a hash of attribute/value pairs added to the resulting html tag.

So when you do:

link\_to “Results”,  
:action =\> “index”,  
:id =\> “current”,  
:class =\> “northcrest”

Ruby thinks it is:

link\_to(“Results”,  
{:action =\> “index”,  
:id =\> “current”,  
:class =\> “northcrest”})

All those options are getting wrapped up in the hash that is used to  
generate the url. You want to pass some of those into the html\_options  
argument.

link\_to(‘Results’, {:action =\> 'index},  
{:id =\> ‘current’, :class =\> 'northcrest})

This nested brace syntax can be a bit cumbersome. This is one of the  
many reasons people often used named routes instead.

#routes.rb  
map.results ‘results’, :controller =\> ‘results’

#view  
link\_to ‘Results’, results\_path, :id =\> ‘current’, :class =\>  
‘northcrest’

The results\_path fills the url portions of the arguments, and the hash  
you then pass in falls into the “html\_options” argument, providing the  
functionality you want.

---

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [May 29, 2008, 12:25am UTC](https://www.ruby-forum.com/t/setting-classes-and-ids/138918/5 "2008-05-29T00:25:33Z")

</div>

> hey thanks for the reply. i tried what you suggested. here is the  
> code:
> 
> \<%= link\_to “Results”, :action =\> “index”, :id =\> “current”, :class =\>  
> “northcrest” %\>

oops.

\<%= link\_to “Results”, {:action =\> “index”}, :id =\> “current”, :class =\>  
“northcrest” %\>

(note the {}'s)

---

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [May 29, 2008, 12:49am UTC](https://www.ruby-forum.com/t/setting-classes-and-ids/138918/6 "2008-05-29T00:49:54Z")

</div>

that worked it out! thanks so much. you’ve been a great help.

Alex W. wrote:

> Check out the docs for link\_to
> 
> [http://caboo.se/doc/classes/ActionView/Helpers/UrlHelper.html#M006132](http://caboo.se/doc/classes/ActionView/Helpers/UrlHelper.html#M006132)
