To assign a css class or id to a link_to method you do the following:
<%= link_to "Link name", { :controller => 'controller_name', :action => 'action_name' }, :class => 'someclass' -%>
The key part is seperating the :controller => and :action from the html using the curly brackets.
Comments left...
cheers :)
sal at 14 Jun 07 at 22:34
thanks!
c0ureur at 25 Sep 07 at 10:04
Thanks
caius at 01 Dec 07 at 21:28
Thank you.
Mike at 03 Dec 07 at 12:34
Not sure why this isn’t isn’t listed in the api site: http://api.rubyonrails.org but it does explain why you’re site was at the top of the list.. thanks!
Ryan at 04 Dec 07 at 18:19
I would like to add a class to a single link. My problem is that I am getting those links from a loop:
< @links.each do |link| > < link_to link.name, {}, :class=>‘red’ > < end >
I would appreciate any help to do that? Thank you.
zangra at 17 Dec 07 at 13:24
I tried a few times to tack in the class where I also have an image_tag inside the link_to … without luck so far..I’ll come back with an answer if I get there !
dreamthinker at 21 Feb 08 at 05:02
This should work:
link_to(image_tag("image.gif"), { :controller => 'controller_name', :action => 'action_name' }, :class => 'someclass')Paul at 21 Feb 08 at 05:09
Thanks, that did it !
Kevin at 28 Feb 08 at 11:17
thank u very much…:)
Francesco at 04 Jul 08 at 09:21
How would you assign multiple classes to an image?
Drew at 19 Sep 08 at 12:54
Dope. You rock – Thanks!
Ron Adair at 19 Sep 08 at 21:05
That was very useful.Thanks!!!!!
Zeba at 12 Nov 08 at 02:47
Thanks a lot man!!!
Paras Mendiratta at 21 Nov 08 at 18:24
Thanks man !!!!!!!!!!
SpainAndFrance at 27 Apr 09 at 10:06
Thanks, but how to pass html code to link_to with block?
< link_to root_path, ?html_code? > ... < end >
nicola at 12 Dec 09 at 12:40
found it… if someone needs it, in order ti pass html attributes to link_to with block you have to call it with parenthesis:
< link_to({:controller => “general”, :action => “act”}, :accesskey => “A”) >
hope this helps
nicola at 12 Dec 09 at 12:45
thanks!
ivan Sidarau at 10 Feb 10 at 14:42
thank y very much, was what i wanted
boliboli at 11 Mar 10 at 00:34
Articles Archive →
Got something to say?