Forum: Ruby on Rails Please help : how to link_to() and css styles ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
77707eb62b77a12638d1d0a9424afdc9?d=identicon&s=25 Nuno (Guest)
on 2006-04-17 13:21
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 :
<a href="/gallery/list?class=btn-cancel">Cancel</a>


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


Thanks
A1c4a0bb61649f18a4d65b1c7273a50f?d=identicon&s=25 Christian Klauser (sealedsun)
on 2006-04-17 13:27
> <%= 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' %>
77707eb62b77a12638d1d0a9424afdc9?d=identicon&s=25 Nuno (Guest)
on 2006-04-17 17:35
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 !?
D2a5b7975f49e051c1de10f98ea81f63?d=identicon&s=25 Chang Sau Sheong (Guest)
on 2006-04-17 17:48
(Received via mailing list)
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 Sheong

http://blog.saush.com
http://read.saush.com
http://jaccal.sourceforge.net
This topic is locked and can not be replied to.