# Please help : how to link\_to() and css styles?

**URL:** <https://www.ruby-forum.com/t/please-help-how-to-link-to-and-css-styles/57976>\
**Category:** Rails\
**Created:** [April 17, 2006, 1:21pm UTC](https://www.ruby-forum.com/t/please-help-how-to-link-to-and-css-styles/57976 "2006-04-17T13:21:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [April 17, 2006, 1:21pm UTC](https://www.ruby-forum.com/t/please-help-how-to-link-to-and-css-styles/57976/1 "2006-04-17T13:21:05Z")

</div>

Hello, I’m having hard times on a very simple problem : how to provide a  
css class to a link\_to tag ?

I’m using this :  
\<%= link\_to ‘Cancel’, :action =\> ‘list’, :class =\> ‘btn-cancel’ %\>

and RoR produces this :  
[Cancel](https://www.ruby-forum.com/gallery/list?class=btn-cancel)

I’ve read the doc of url\_for several times but I still don’t see why it  
doesn’t work ?

Thanks

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [April 17, 2006, 1:27pm UTC](https://www.ruby-forum.com/t/please-help-how-to-link-to-and-css-styles/57976/2 "2006-04-17T13:27:57Z")

</div>

> \<%= link\_to ‘Cancel’, :action =\> ‘list’, :class =\> ‘btn-cancel’ %\>

Definition of link\_to:

```
link_to(title, url, options)

```

The ruby interpreter thinks that :class belongs to the url. Try:

\<%=link\_to Cancel, {:action =\> ‘list’}, :class =\> ‘btn-cancel’ %\>

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [April 17, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/please-help-how-to-link-to-and-css-styles/57976/3 "2006-04-17T17:35:49Z")

</div>

Thanks for the help, it works like a charm but I still don’t understand  
why the 3 comma delimited parameters ‘Cancel’, :action =\> ‘list’, :class  
=\> ‘btn-cancel’ does not fit into title, url, options

Why :action =\> ‘list’, :class =\> ‘btn-cancel’ are interpreted as the  
second parameter ? I thought that {:action =\> ‘list’, :class =\>  
‘btn-cancel’} only would produce that !?

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [April 17, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/please-help-how-to-link-to-and-css-styles/57976/4 "2006-04-17T17:48:26Z")

</div>

I think it’s because if you don’t put braces around your parameters, the  
method thinks that anything after the first parameter is part of the  
second parameter. As a general rule if you have more than 2 parameters  
for link\_to or the like kind of methods, you should put in braces to  
indicate exactly what you want. I believe this is part of Ruby’s  
programming idiom.

Nuno wrote:

> Thanks for the help, it works like a charm but I still don’t understand  
> why the 3 comma delimited parameters ‘Cancel’, :action =\> ‘list’, :class  
> =\> ‘btn-cancel’ does not fit into title, url, options
> 
> Why :action =\> ‘list’, :class =\> ‘btn-cancel’ are interpreted as the  
> second parameter ? I thought that {:action =\> ‘list’, :class =\>  
> ‘btn-cancel’} only would produce that !?

–  
Sau S.

[http://blog.saush.com](http://blog.saush.com)  
[http://read.saush.com](http://read.saush.com)  
[http://jaccal.sourceforge.net](http://jaccal.sourceforge.net)
