Rails link_to and HTML options
Just a small thing, but I needed to know how to do this and it took me a while to find. Hopefully this will help someone else out.
In HTML, we might declare a link like so
<a title="Example" href="/controller/action">Click Here!</a>
Now, in Ruby on Rails, we want to declare a link, so we use the built in link_to method like this
<%= link_to "Click Here!", :controller => "controller", :action => "action" %>
Hmmm… how do we squeeze a title in there? Luckily, the third parameter to link_to is a hash of HTML options.
<%= link_to "Click Here!", {:controller => "controller", :action => "action"}, {:title => "Example"} %>
That should do it. I feel like I still have so much to learn about Ruby on Rails, but the more I learn the more I want to learn!
More of my rantings
- Ruby on Rails Testing – flash.now gotcha
- Ruby on Rails Testing – login gotcha
- Quick Tip: Override Rails Generated URLs
These might also interest you
- Photographer in Red and Blue (Brainfuel)
- Cary Grant and Me (Brainfuel)
- A tree and some grains of grass (Brainfuel)








ah cool that you figured it out. Just an fyi, in many if not all of the ruby method calls which autogenerate html for you, it is possible to pass in any html parameter through what you just did.
So if you know that the ruby call generates a textArea for you and you want to override the max_size or whatever (i don’t know if that’s true), you do it via the parameters. This completely tripped me up for such a long time because I was looking at the ruby api expecting an enumeration of which html attribute I could override until I realized you can pass any in.
October 27th, 2006 at 8:45 pmvery helpful, thanks!!
January 16th, 2010 at 9:45 pmthanks! saved me time searching
March 25th, 2010 at 11:19 pmThanks, this helped clear up how I could send links to other controllers, it seemed simple, but I could never remember the formatting.
July 18th, 2011 at 5:23 amThanks!
August 10th, 2011 at 9:32 amthanks for this info
September 2nd, 2011 at 1:26 pm