How to assign a css class or id to link_to method in Ruby on Rails
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.
About
Paul is a web developer for Kyanmedia web agency. He's lucky enough to write in Ruby on Rails full-time and uses this site to post snippets of code.
Contact
my name at gmail.com
More snippets
Take a look in the archive
Need a website?
Contact my employer. Make sure to check out our portfolio of work.
Hosting
I recommend hostingrails.com
12 comments made
cheers :)
thanks!
Thanks
Thank you.
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!
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.
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 !
This should work:
link_to(image_tag("image.gif"), { :controller => 'controller_name', :action => 'action_name' }, :class => 'someclass')Thanks, that did it !
thank u very much…:)
How would you assign multiple classes to an image?
Dope. You rock – Thanks!
Got something to say?