Please help : how to link_to() and css styles?


#1

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

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

Thanks


#2

<%= 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’ %>


#3

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 !?


#4

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://read.saush.com
http://jaccal.sourceforge.net